嵌套文档查询MongoDB C#

时间:2017-09-18 07:30:08

标签: c# mongodb mongodb-query mongodb-.net-driver

我有以下人员文件

{
name:"John",
address:[
{
street:"1 abc",
city:"delhi"
},
{
street:"1 xyz",
city:"mumbai"
},
]
}

我必须取得所有属于cites delhi和mumbai的人

我使用MongoDB C#2.4驱动程序编写了查询

string[] cities = new string[] { "delhi", "mumbai" };
var cityFilter =Builders<Person>.Filter.Eq("address.city",cities); 
   var person = database.GetCollection<Person>("Person").Find(cityFilter).ToList();

查询无异常执行,但没有得到任何结果。

1 个答案:

答案 0 :(得分:0)

这是解决方案:

var cityFilter = Builders<Person>.Filter.ElemMatch(y => y.addresses, x => cities.Contains(x.city));