Neil Lunn的解决方案有效,但我有另一种方法,因为
$lookup pipe不支持“from”语句中的shard集合。
所以我以前使用的是简单的Java脚本。它简单且易于修改。但对于性能,您应该有适当的索引!
var mycursor = db.collA.find( {}, {_id: 0, myId:1} )
mycursor.forEach( function (x){
var out = db.collB.count( { yourId : x.myId } )
if ( out > 0) {
print('The id exists! ' + x.myId); //debugging only
//put your other query in here....
}
} )