0
votes

I am having a question regarding to filter the documents whose specific field is not in a list of values. For example, documents only contain "_id" field which is unique and indexed. Now I have a list of "_id"s and like to find out all the documents whose "_id" are not in the list of "_id"s.

I can do it by using $in operator like:

db.mycollection.find({_id:{$in:["id1","id2"]}},{_id:1})

to return all documents whose ids are in the list then compare with the given list. The performance of this way is not good enough for thousands of documents. I am wondering if there is better way to do this. For example is it possible to construct a query to only return the documents whose ids are not in the list to reduce the network transfer time?

Thanks

1

1 Answers

1
votes

$nin is what you are looking for

db.mycollection.find({ "_id": { "$nin": [ "id1","id2" ]}}, { "_id": 1 })