MongoDB $oid vs ObjectId

▼魔方 西西 提交于 2020-01-01 08:46:19

问题


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

{
"_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:

How to create query with ObjectIds using java?

$all parameter in mongodb does not work with ObjectId list


回答1:


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

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


来源:https://stackoverflow.com/questions/26938598/mongodb-oid-vs-objectid

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!