42

i found the way to check is the value contains in simple array :

var filter = Builders<Post>.Filter.AnyEq(x => x.Tags, "mongodb");

But how to find a complex item with many fields by a concrete field ? I found the way to write it via the dot notation approach with BsonDocument builder, but how can i do it with typed lambda notations ?

upd

i think it some kind of

builderInst.AnyIn(p => p.ComplexCollection.Select(ml => ml.Id), mlIds)

but can't check right now, is anyone could help ?

Vladyslav Furdak
  • 1,765
  • 2
  • 22
  • 46
  • Looks like a homework from MongoDb University. – Evgeni Nabokov Oct 31 '15 at 21:51
  • 1
    Possible duplicate of [MongoDB + C# driver + query array of elements where each array element contains sub-document to query on](http://stackoverflow.com/questions/12024087/mongodb-c-sharp-driver-query-array-of-elements-where-each-array-element-cont) – The Bearded Llama Feb 01 '17 at 18:00

4 Answers4

56

There is ElemMatch

var filter = Builders<Post>.Filter.ElemMatch(x => x.Tags, x => x.Name == "test");
var res = await collection.Find(filter).ToListAsync()
rnofenko
  • 9,198
  • 2
  • 46
  • 56
  • This is not an optimal solution. The best solution is to use http://api.mongodb.com/csharp/current/html/M_MongoDB_Driver_FilterDefinitionBuilder_1_AnyIn__1_1.htm – shiva8 Oct 25 '16 at 15:37
  • 4
    @shiva8 You can not user `AnyIn` when you have array of complex objects. – rnofenko Oct 26 '16 at 17:45
15

Here's an example that returns a single complex item from an array (using MongoDB.Driver v2.5.0):

Simple Data Model

public class Zoo
{
    public List<Animal> Animals { get; set; }
}

public class Animal
{
    public string Name { get; set; }
}

Option 1 (Aggregation)

public Animal FindAnimalInZoo(string animalName)
{
    var zooWithAnimalFilter = Builders<Zoo>.Filter
        .ElemMatch(z => z.Animals, a => a.Name == animalName);

    return _db.GetCollection<Zoo>("zoos").Aggregate()
        .Match(zooWithAnimalFilter)
        .Project<Animal>(
            Builders<Zoo>.Projection.Expression<Animal>(z => 
                z.Animals.FirstOrDefault(a => a.Name == animalName)))
        .FirstOrDefault(); // or .ToList() to return multiple
}

Option 2 (Filter & Linq) This was about 5x slower for me

public Animal FindAnimalInZoo(string animalName)
{
    // Same as above
    var zooWithAnimalFilter = Builders<Zoo>.Filter
        .ElemMatch(z => z.Animals, a => a.Name == animalName);

    var zooWithAnimal = _db.GetCollection<Zoo>("zoos")
        .Find(zooWithAnimalFilter)
        .FirstOrDefault();

    return zooWithAnimal.Animals.FirstOrDefault(a => a.Name == animalName);
}
JBond
  • 159
  • 1
  • 3
8

You need the $elemMatch operator. You could use Builders<T>.Filter.ElemMatch or an Any expression:

Find(x => x.Tags.Any(t => t.Name == "test")).ToListAsync()

http://mongodb.github.io/mongo-csharp-driver/2.0/reference/driver/expressions/#elemmatch

hansmaad
  • 18,417
  • 9
  • 53
  • 94
  • 3
    Warning, Any() doesn't work when targeting Azure CosmosDB - see https://feedback.azure.com/forums/263030-azure-cosmos-db/suggestions/33755596-support-dot-notation-with-mongodb – LMK Nov 19 '18 at 01:38
4

As of the 2.4.2 release of the C# drivers, the IFindFluent interface can be used for querying on array element. ElemMatch cannot be used on an array of strings directly, whereas the find interface will work on either simple or complex types (e.g. 'Tags.Name') and is strongly typed.

            FilterDefinitionBuilder<Post> tcBuilder = Builders<Post>.Filter;
            FilterDefinition<Post> tcFilter = tcBuilder.Eq("Tags","mongodb") & tcBuilder.Eq("Tags","asp.net");
               ...
            await myCollection.FindAsync(tcFilter);

Linq driver uses the aggregation framework, but for a query with no aggregation operators a find is faster.

Note that this has been broken in previous versions of the driver so the answer was not available at the time of original posting.

Aaron Newman
  • 549
  • 1
  • 5
  • 27
  • Hello Aaron, your solution had fixed my problem.However, i would like to know the performance of this, if there are close to a lakh records. – Surya Deepak Jan 13 '19 at 07:59