MongoDB $oid 与 ObjectId

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26938598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:12:20  来源:igfitidea点击:

MongoDB $oid vs ObjectId

mongodbmlab

提问by Vlad Vinnikov

I'm trying to get mongodb query working. Collection comes in the format:

我正在尝试让 mongodb 查询工作。集合采用以下格式:

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

The following works:

以下工作:

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

However, I get a syntax error for the following:

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

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

There are a similar questions that suggested that ObjectId should work:

有一个类似的问题表明 ObjectId 应该可以工作:

How to create query with ObjectIds using java?

如何使用java创建带有ObjectIds的查询?

$all parameter in mongodb does not work with ObjectId list

mongodb 中的 $all 参数不适用于 ObjectId 列表

回答by jared

The MongoLab UI uses Strict MongoDB Extended JSONso Object IDs are represented thusly, as in the second code block of the OP:

MongoLab UI 使用Strict MongoDB Extended JSON,因此对象 ID 以这种方式表示,如 OP 的第二个代码块所示:

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