我有以下数据:
/* 1 */
{
"_id" : 1,
"FirstName" : "John",
"LastName" : "Doe",
"Test" : [{
"TestId" : ObjectId("5046109daedabb0cf8800f3f"),
"DateTaken" : ISODate("2012-09-10T15:46:32.866Z")
}, {
"TestId" : ObjectId("506e0928aedabb087c4ec9d1"),
"DateTaken" : ISODate("2011-06-26T18:05:52.625Z"),
"Responses" : [{
"AnswerId" : "506e0928aedabb087c4ec98c",
"QuestionId" : ObjectId("506e0928aedabb087c4ec98b"),
"ResponseDate" : ISODate("2011-06-26T04:00:00Z")
}]
}]
}
/* 2 */
{
"_id" : 2,
"FirstName" : "Jane",
"LastName" : "Doe",
"TestHistory" : [{
"TestId" : ObjectId("506e0928aedabb087c4ec9d1"),
"DateTaken" : ISODate("2011-06-26T18:05:52.625Z"),
"Responses" : [{
"AnswerId" : "506e0928aedabb087c4ec98c",
"QuestionId" : ObjectId("506e0928aedabb087c4ec98b"),
"ResponseDate" : ISODate("2011-06-26T04:00:00Z")
}]
}]
}
我正在尝试编写一个查询,返回存在没有响应的Test的所有实例。我尝试了以下查询:
- {“Test.Responses”:{$ exists:false}}
- {$ and:[{Test:{$ exits:true}},{“Test.Responses”:{$ exists:false}}]}
醇>
两者都返回0结果。我错过了什么?
答案 0 :(得分:1)
我认为您正在寻找的是:
db.foo.find({
Test: { $elemMatch: {
Responses: { $exists: false }
}
})
否则,您的查询会查找Test
下的所有文档都没有Responses
属性的记录。