2012-09-29 34 views
2

对象的字段数组值如果我有这样一个类:查询MongoDB中

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"数组,并在数组中找到匹配的对象并返回整个类。

回答

2

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

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

它将返回所有在Types数组中等于字符串“New”的条目的文档。请查询the documentation了解更多信息。