Mongo:按键一级深度查询

axi*_*x11 19 mongodb

是否可以在Mongo查询中使用类似"exists"的内容来根据ID返回此记录?

'ids'包含关键字'123456'的选项

  {
      "department": "Digging",
      "ids": {
        "123456": {
          "color": "blue"
        },
        "123457": {
          "color": "red"
        }
      }
    }
Run Code Online (Sandbox Code Playgroud)

Ada*_*Dev 21

当您搜索具有给定名称的字段时,$ exists是您需要的运算符(请参阅高级查询).

例如:

db.YourCollection.find({ "ids.123456" : {$exists: true}});
Run Code Online (Sandbox Code Playgroud)