2

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.

1 Answer 1

2

The MongoDB query language can access arrays transparently. So you can just do:

db.cars.find({Types:"New"})

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.