1

I have an array of ObjectIDs, 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?

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I ended up using the second option you specified.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.