Home:ALL Converter>MongoDB: Efficient $nin operator

MongoDB: Efficient $nin operator

Ask Time:2018-12-07T15:22:22         Author:Arsen Davtyan

Json Formatter

I have two collections, they both have one common field:

collection1

{
    "myCommonFieldId" : 1
}

{ 
    "myCommonFieldId" : 2
}

{
    "myCommonFieldId" : 3
}

{
    "myCommonFieldId" : 4
}

{
    "myCommonFieldId" : 5
}

collection2

{
    "myCommonFieldId" : 2
}

{
    "myCommonFieldId" : 5
}

I want to get documents from collection1 which are not in collections2 (The uncommon myCommonFieldId-s between two collections). In this case:

{
    "myCommonFieldId" : 1
}

{
    "myCommonFieldId" : 3
}

{
    "myCommonFieldId" : 4
}

The straightforward solution is to query collection2, take the results and run $nin in collection1

db.collection2.find({}); 
db.collection1.find({"myCommonFieldId" : {$nin : [<Result from query1>]}})

I have a huge dataset and since $nin operator is not efficient, $nin is becoming very expensive operation for me: Quoting from MongoDB documentations:

The inequality operator $nin is not very selective since it often matches a large portion of the index. As a result, in many cases, a $nin query with an index may perform no better than a $nin query that must scan all documents in a collection.

Is there any efficient way to do this?

Author:Arsen Davtyan,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/53664986/mongodb-efficient-nin-operator
yy