Search code examples
c#mongodbmongodb-querymongodb-.net-driver

How to filter on an array of objects that equals name and contains value?


In my MongoDB collection, I have an array of objects that look like this:

[
  {
    "_id": 123,
    "info": [
      {
        "name": "John",
        "random": ["31", "food", "sleep"]
      }
    ]
  },
  {
    "_id": 234,
    "info": [
      {
        "name": "Amy",
        "random": ["tv", "food", "sleep"]
      }
    ]
  },
  {
    "_id": 345,
    "info": [
      {
        "name": "John",
        "random": ["cars", "tv31", "sleep"]
      }
    ]
  }
]

In C# how would I filter on info where name is John and random contains 31? I also don't know the difference between AnyIn and In.


Solution

  • The LINQ query (which is covered by other answerers) works and is easier for those unfamiliar with the MongoDB Fluent query.


    For those who are looking for MongoDB Fluent syntax:

    Assume that your model classes are as below:

    public class RootModel
    {
       [BsonId]
       public int Id { get; set; }
    
       public InfoModel[] Info { get; set; }
    }
    
    [BsonNoId]
    public class InfoModel
    {
        public string Name { get; set; }
    
        public string[] Random { get; set; }
    }
    

    You should work with .ElemMatch and .AnyIn:

    var filter = Builders<RootModel>.Filter.ElemMatch(x => x.Info,
            Builders<InfoModel>.Filter.Eq(y => y.Name, "John")
            & Builders<InfoModel>.Filter.AnyIn(y => y.Random, new string[] { "31" }));
    

    For the Post Owner's question on the differences between .In and .AnyIn:

    In works when your field is a single value and is used to match any element from the provided input array.

    AnyIn works when your field is an array and is used to match any element from the provided input array.


    Based on your scenario, you will get the syntax error when using .In, for the implementation of In as below:

    public FilterDefinition<TDocument> In<TField>(Expression<Func<TDocument, TField>> field, IEnumerable<TField> values)
    {
        return In(new ExpressionFieldDefinition<TDocument, TField>(field), values);
    }
    

    Unless you are checking the whole Random array that is exactly matched with the array, for example:

    random: {
       $in: [['31', 'food', 'sleep'], ...]
    }
    

    So this works with In:

    Builders<InfoModel>.Filter.In(y => y.Random, 
        new string[][] { new string[] { "31", "food", "sleep" } })
    

    That is another story matching the exact array value compared to the current question which asks for matching any element in the array.