Get “data from collection b not in collection a” in a MongoDB shell query

后端 未结 4 2005
执笔经年
执笔经年 2020-12-23 14:06

I have two MongoDB collections that share a common _id. Using the mongo shell, I want to find all documents in one collection that do not have a matching _id in the other co

4条回答
  •  盖世英雄少女心
    2020-12-23 14:58

    I've made a script, marking all documents on the second collection that appears in first collection. Then processed the second collection documents.

    var first = db.firstCollection.aggregate([ {'$unwind':'$secondCollectionField'} ])
    
    while (first.hasNext()){ var doc = first.next(); db.secondCollection.update( {_id:doc.secondCollectionField} ,{$set:{firstCollectionField:doc._id}} ); }
    

    ...process the second collection that has no mark

    db.secondCollection.find({"firstCollectionField":{$exists:false}})
    

提交回复
热议问题