Mongo:按键一级深度查询

时间:2012-03-19 18:56:04

标签: mongodb

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

那样选择'ids'包含关键字'123456'的位置?

  {
      "department": "Digging",
      "ids": {
        "123456": {
          "color": "blue"
        },
        "123457": {
          "color": "red"
        }
      }
    }

1 个答案:

答案 0 :(得分:19)

当您搜索是否存在具有给定名称的字段时,$ exists是您需要的运算符(请参阅Advanced Queries)。

e.g。类似的东西:

db.YourCollection.find({ "ids.123456" : {$exists: true}});