I have an array of ObjectID
s, representing _id
values for documents in a collection:
ObjectID[] documentsAffected
How can I prepare a query to return all the documents that are referenced in this array?
I have looked at the following but it only works with individual values, not arrays:
Query.EQ("_id", documentsAffected)
I don't want to write a for loop and have to return one document at a time either, because I then need to update these documents in the same way and this seems inefficient when I could update them all in one statement after retrieving them all.
Is there a way to effectively do this? If not, is there another way to return all documents based on some list of items that reference them?
You can use In
instead of EQ
:
Query.In("_id", documentsAffected);
Or better off, using the typed options:
Query<Document>.In(doc => doc.Id, documentsAffected);
This will create a query using the $in
operator:
The $in operator selects the documents where the value of a field equals any value in the specified array. If the field holds an array, then the $in operator selects the documents whose field holds an array that contains at least one element that matches a value in the specified array
From $in