1

我有以下数据:

/* 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")
        }]
    }]
}

我正在尝试编写一个查询,该查询返回存在没有响应的测试的所有实例。我尝试了以下查询:

  1. { "Test.Responses": { $exists: false } }
  2. { $and: [ { Test: { $exits: true } }, { "Test.Responses": { $exists: false} } ] }

两者都返回 0 结果。我错过了什么?

4

1 回答 1

1

我认为您正在寻找的是:

db.foo.find({
    Test: { $elemMatch: {
        Responses: { $exists: false }
    }
})

否则,您的查询将查找所有文档Test都不具有该Responses属性的记录。

于 2013-03-05T19:33:54.387 回答