MongoDB $ oid与ObjectId [英] MongoDB $oid vs ObjectId

查看:2201
本文介绍了MongoDB $ oid与ObjectId的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使mongodb查询工作.集合的格式为:

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"
}

以下作品:

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

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

However, I get a syntax error for the following:

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

还有一个类似的问题表明ObjectId应该起作用:

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

如何使用Java使用ObjectId创建查询?

$ mongodb中的所有参数均不适用于ObjectId列表

推荐答案

MongoLab UI使用严格的MongoDB扩展的JSON ,因此对象ID以此表示,如OP的第二个代码块所示:

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>" }

这篇关于MongoDB $ oid与ObjectId的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆