Mongodb查询嵌套文档

时间:2014-05-27 10:32:34

标签: mongodb mongodb-query

我正在尝试提出一个mongo查询,该查询将返回特定时间范围内的文档和仅字段(嵌套文档中的更改)。

这是我尝试过的,但不幸的是,这并不起作用。

感谢您的帮助!

查询:

   db.keywords.find(
        {
            $and :[{"_id" : "5374c7a7ac58b0d3b5e970fa"},{"field" : "keywordId"},{"adwordsChanges.timestamp" : {$gte:1400162491325, $lte:23918779329}}]
        })

文件:

{
   {
    "_id": ObjectId("5374c7a7ac58b0d3b5e970fa"),
    "documentchanges": [{
        "timestamp": NumberLong("1400162491325"),
        "field": "syncState",
        "old": null,
        "new": "OK"
        }, {
        "timestamp": NumberLong("1400162491325"),
        "field": "keywordId",
        "old": null,
        "new": NumberLong("23918779329")
        }, {
        "timestamp": NumberLong("1400162491325"),
        "field": "adGroupId",
        "old": null,
        "new": NumberLong("16972286472")
        }]
    }, {
    "_id": ObjectId("5374c7a7ac58b0d3b5e970f9"),
    "documentchanges": [{
        "timestamp": NumberLong("1400162491325"),
        "field": "syncState",
        "old": null,
        "new": "OK"
        }, {
        "timestamp": NumberLong("1400162491325"),
        "field": "keywordId",
        "old": null,
        "new": NumberLong("23918779329")
        }, {
        "timestamp": NumberLong("1400162491325"),
        "field": "adGroupId",
        "old": null,
        "new": NumberLong("16972286472")
        }]
    }
}

1 个答案:

答案 0 :(得分:2)

您的_id查找正在查找字符串,但您的对象禁止使用ObjectId。使用此:

{"_id" : ObjectId("5374c7a7ac58b0d3b5e970fa")}

您在.之前缺少field运算符。使用方法:

{"documentchanges.field" : "keywordId"}

您的顶级文档中也没有标记adwordsChanges,但您在此处进行了处理:

{"adwordsChanges.timestamp" : {$gte:1400162491325, $lte:23918779329}}

将此更改为documentchanges

{"documentchanges.timestamp" : {$gte:1400162491325, $lte:23918779329}}

此外,没有时间戳小于或等于23918779329.更改号码。