MongoDB $ oid vs ObjectId

时间:2014-11-14 20:48:35

标签: mongodb mlab

我正在尝试让mongodb查询正常工作。收集的格式为:

{
"_id": {
    "$oid": "54651022bffebc03098b4567"
},
"browser": "ie",
"browser_version": "10.0 Desktop",
"os_version": "8",
"device": null,
"os": "Windows"
}

以下作品:

{
    "_id": {
        "$in": [
            {
                "$oid": "54651022bffebc03098b4567"
            },
            {
                "$oid": "54651022bffebc03098b4568"
            }
        ]
   }
}

但是,我收到以下语法错误:

{
    "_id": {
        "$in": [
            ObjectId("54651022bffebc03098b4567"),
            ObjectId("54651022bffebc03098b4568")
        ]
    }
}

有类似的问题表明ObjectId应该有效:

How to create query with ObjectIds using java?

$all parameter in mongodb does not work with ObjectId list

1 个答案:

答案 0 :(得分:19)

MongoLab UI使用Strict MongoDB Extended JSON,因此对象ID就如此表示,如OP的第二个代码块:

{ "$oid": "<id>" }