我正在使用官方MongoDB C#驱动程序的1.5.0.4566版.我正在使用Mongo 2.06版.
这是我的文档结构的样子(省略了本讨论不需要的字段):
{ "Parents" :
[
{
"CreatedOn": ISODate("2012-07-28T15:30:06.623Z"),
"Title": "Simple Title",
"Children": [
{ "StatusId": 1, "Active" : true, SubChild : { "ExpiresOn": ISODate("2012-07-28T15:30:06.623Z")}},
{ "StatusId": 1, "Active" : true, SubChild : { "ExpiresOn": ISODate("2012-08-28T15:30:06.623Z")}}
]
},
{
"CreatedOn": ISODate("2012-07-28T15:30:06.623Z"),
"Title": "Another Simple Title",
"Children": [
{ "StatusId": 1, "Active" : true, SubChild : { "ExpiresOn": ISODate("2012-07-28T15:30:06.623Z")}},
{ "StatusId": 1, "Active" : true, SubChild : { "ExpiresOn": ISODate("2012-08-28T15:30:06.623Z")}}
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
如果我想查询StatusId等于1并且Active为true的Children,我可以使用ElemMatch.
Query.ElemMatch("Children", Query.And(Query.EQ("StatusId", 1),Query.EQ("Active",true)));
Run Code Online (Sandbox Code Playgroud)
我无法工作的是当我需要在查询中包含SubChild元素时. …