0

I am using the C# driver for Mongodb and I was wondering how to find any documents with an ID (int) that correspond to a list of IDs(List).

I know theoretically, if I was checking a list in each document for a single int I could use "EqAny(c=>c.IDs, 32)" however I am looking for the opposite of that where I can check a single int on the document to see if it matches with any of a list of ints.

My current code is this, which returns all documents due to the line "var filter = FilterDefinition.Empty;"

public void GetSerialsForZones(List<int> zoneIds)
{
    IMongoDatabase db = MongoClient.GetDatabase(DatabaseName);
    IMongoCollection<ZoneMessage> collection = db.GetCollection<ZoneMessage>(CollectionName);

    var filter = FilterDefinition<ZoneMessage>.Empty;
    foreach (int zoneId in zoneIds)
        filter &= Builders<ZoneMessage>.Filter.Eq(c => c.ID, zoneId);

    var result = await collection.Find(filter).ToListAsync();
}

The code has been modified for simplification.

  • Just tried that and `.Where` doesn't work because the IMongoCollection is not enumerable I don't think. – R. Bell Feb 17 '20 at 14:57
  • Have a look at https://stackoverflow.com/questions/41490392/use-linq-in-c-sharp-to-find-mondodb-records-when-values-in-a-list-field-match-a – MKR Feb 17 '20 at 15:08

0 Answers0