我使用的是官方 MongoDB C# 驱动程序的 1.5.0.4566 版本。MongoDB 的版本号为 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")}}
]
}
]
}
如果我想查询状态ID等于1且活动状态为真的孩子,我可以使用ElemMatch。
Query.ElemMatch("Children", Query.And(Query.EQ("StatusId", 1),Query.EQ("Active",true)));
我无法使查询包含SubChild元素。
Query.ElemMatch("Children", Query.And(Query.EQ("StatusId",1), Query.EQ("Active",true),Query.LT("SubChild.ExpiresOn",DateTime.UtcNow)));
当我尝试在查询中包含SubChild.ExpiresOn字段时,查询没有返回任何值。我已经尝试了不同的构建查询的方法,但每次在查询中包含SubChild.ExpiredOn字段时都会得到零个文档。
我错过了什么吗?
谢谢,
Parents
实际上是一个集合而不是文档)。 - kelloti