0

I am using mongodb with the C# driver. I have an object (document) with a property of type List<ObjectId> (say arrProp).

I want to find all documents that the arrProp contains at least one element from a given array (another List<ObjectId>).

Is there a way to get the result in a single query?

1
  • Please post some code of what you have attempted so far. Commented Feb 22, 2015 at 21:37

1 Answer 1

2

If you are using driver version 1.5 or later, you can simply use the contains predicate as described in this issue

https://jira.mongodb.org/browse/CSHARP-462

If you are using a driver that is pre 1.5, you can use the following extension

ContainsAny (LINQ to MongoDB extension method)

This method is used to test whether an array (or array-like) field or property contains any of the provided values.

var query =
    from c in collection.AsQueryable<C>()
    where c.A.ContainsAny(new[] { 1, 2, 3 })
    select c;
// or
var query =
    collection.AsQueryable<C>()
    .Where(c => c.A.ContainsAny(new[] { 1, 2, 3 }));
Sign up to request clarification or add additional context in comments.

Comments

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.