0

Let's say I have this document.

"Id" : "lot1",
"Fruits" : [
       [{ "Id": "fruit1", "Name": "apple"}, { "Id": "fruit2", "Name": "carrot"}]
       [{ "Id": "fruit3", "Name": "banana"}]
     ]

Q: How can I query the Fruits array by a list of fruits names ?

I have tried like this:

var fruitNames = new List<string>(){ "apple", "banana" };
var builder = Builders<Lot>.Filter;
var filter = builder.AnyIn(l => l.Fruits.Select(f => f.Name), fruitNames); //TAKE 1
var filter = builder.Where(l => l.Fruits.Select(f => f.Name).Any(f => fruitNames.Contains(f)));//TAKE 2
var filter = builder.AnyIn("Fruits.Name", fruitNames );//TAKE 3
var results = mongoContext.Lots.Find(filter).ToList();

I have tried this in 3 different ways with no success.

0

1 Answer 1

2

Can you try this? It is a little hack but i am pretty sure it will work:

var filter= Builders<Lot>.Filter.ElemMatch(y => y.fruits, x => fruitNames.Contains(x.name));
Sign up to request clarification or add additional context in comments.

2 Comments

If the current "Fruits" array would be an singular object, how would the query modify ? var filter = Builder<Lot>.In(l => l.Fruits.Name, fruitNames); // doesn't work
did you sort it out or you still need help @Misi?

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.