在MongoDB中查询对象的字段数组值

时间:2012-09-29 07:24:55

标签: mongodb mongodb-.net-driver

如果我有这样的课程:

public class Car {
 public string Model { get; set; }
 public List<string> Types { get; set; }
}

并且做:

Car _car = new Car();
_car.Model = "1992";
List<string> _types = new List<string>() { "New", "Old" };
_car.Types = _types

并在MongoDB中保存这些类型的对象,如何在C#MongoDB中获得所有type == "New"的汽车?我需要查询Car.Type == "New"我将访问该类并查看其"Types"数组并在数组中找到匹配的对象并返回整个类。

1 个答案:

答案 0 :(得分:2)

MongoDB查询语言可以透明地访问数组。所以你可以这样做:

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

它将返回所有在Types数组中具有条目的文档,该条目等于字符串“New”。查看the documentation了解详情。