YMC YMC - 1 year ago 72
C# Question

query to find doc with at least one of its array element matches certain condition?

Let's say I have documents like this in mongodb:

"cities" : ["san jose", "san francisko", "new york"],
"cities" : ["santa clara", "seattle"],

How to write a query to find all documents which [cities] array has at least one element starting with given value like "san"? Preferably in c# driver model

Answer Source

MongoDB.Driver provides Regex method which can accept collections as a first argument, regex expression as a second argument

var filter = Builders<Post>.Filter.Regex(x => x.Cities, "texttttt");
return collection.Find(filter).ToListAsync();