If I have a class like this:
public class Car {
public string Model { get; set; }
public List<string> Types { get; set; }
}
and do:
Car _car = new Car();
_car.Model = "1992";
List<string> _types = new List<string>() { "New", "Old" };
_car.Types = _types
and save these kinds of objects in MongoDB, how do I get all cars that have type == "New" in C# MongoDB? I need to query Car.Type == "New" I’m going to visit the class and look in its "Types" array and find the matching object in the array and return the whole class.
The MongoDB query language can access arrays transparently. So you can just do:
and it will return all documents which have an entry in the Types array which is equal to the string “New”. Check the documentation for more information.