【问题标题】:In Mongo query in Subdocuments multiple fields在子文档中的 Mongo 查询中,多个字段
【发布时间】:2017-02-19 20:12:59
【问题描述】:

我在 Mongo 有一个收藏

{
  "_id": 1,
  "favorites": {
    "artist": "Picasso",
    "food": "pizza"
  },
  "finished": [
    17,
    3
  ],
  "badges": [
    "blue",
    "black"
  ],
  "points": [
    {
      "points": 85,
      "bo nus": 20
    },
    {
      "points": 85,
      "bonus": 10
    }
  ]
}{
  "_id": 2,
  "favorites": {
    "artist": "Miro",
    "food": "meringue"
  },
  "finished": [
    11,
    25
  ],
  "badges": [
    "green"
  ],
  "points": [
    {
      "points": 85,
      "bonus": 20
    },
    {
      "points": 64,
      "bonus": 12
    }
  ]
}{
  "_id": 3,
  "favorites": {
    "artist": "Cassatt",
    "food": "cake"
  },
  "finished": [
    6
  ],
  "badges": [
    "blue",
    "red"
  ],
  "points": [
    {
      "points": 85,
      "bonus": 8
    },
    {
      "points": 55,
      "bonus": 20
    }
  ]
}{
  "_id": 4,
  "favorites": {
    "artist": "Chagall",
    "food": "chocolate"
  },
  "finished": [
    5,
    11
  ],
  "badges": [
    "red",
    "black"
  ],
  "points": [
    {
      "points": 53,
      "bonus": 15
    },
    {
      "points": 51,
      "bonus": 15
    }
  ]
}{
  "_id": 5,
  "favorites": {
    "artist": "Noguchi",
    "food": "nougat"
  },
  "finished": [
    14,
    6
  ],
  "badges": [
    "orange"
  ],
  "points": [
    {
      "points": 71,
      "bonus": 20
    }
  ]
}{
  "_id": 6,
  "favorites": {
    "food": "pizza",
    "artist": "Picasso"
  },
  "finished": [
    18,
    12
  ],
  "badges": [
    "black",
    "blue"
  ],
  "points": [
    {
      "points": 78,
      "b onus": 8
    },
    {
      "points": 57,
      "bonus": 7
    }
  ]
}

我想检索所有具有 points = 85 和 bonus = 20 的元素。 查询将是

db.temp2.find({"points":{"points":85,"bonus":20}})

它返回 id 为 1 和 2 的文档。

现在,如果我想检索具有 (点数 = 85 和奖励 = 20) 的元素以及另一个具有 {点数 = 85 和奖励 > 10 的子文档)。基本上我想检索 id = 2 的元素

如果查询是

 db.temp2.find({$and:[{"points":{"points":85,"bonus":20}},{"points":{"points":64,"bonus":{$gte:10}}}]}).pretty()

查询时没有结果

 db.temp2.find({$and:[{"points":{"$elemMatch":{"points":85,"bonus":20}}},{"points":{"$elemMatch":{"points":64,"bonus":{$gte:10}}}}]})

给我 id=2。

我用花药组尝试过同样的事情

[
  {
    "name": "User1",
    "tags": [
      {
        "k": "group",
        "v": "test"
      },
      {
        "k": "color",
        "v": "blue"
      }
    ]
  },
  {
    "name": "User2",
    "tags": [
      {
        "k": "group",
        "v": "dev"
      },
      {
        "k": "color",
        "v": "blue"
      }
    ]
  },
  {
    "name": "User3",
    "tags": [
      {
        "k": "group",
        "v": "dev"
      },
      {
        "k": "color",
        "v": "red"
      }
    ]
  }
]

如果你想找出具有

的元素
"tags": [
      {
        "k": "group",
        "v": "dev"
      },
      {
        "k": "color",
        "v": "blue"
      }
    ] 

查询:

db.temp4.find({$and:[{"tags":{"k":"group","v":"dev"}},{"tags":{"k":"color","v":"blue"}}]})

db.temp4.find({$and:[{"tags":{"$elemMatch":{"k":"group","v":"dev"}}},{"tags":{"$elemMatch":{"k":"color","v":"blue"}}}]})

在这两种情况下,您都会得到响应。

请帮助我了解何时使用 $elemMatch"$and"

提前致谢。 抱歉语法错误。

【问题讨论】:

    标签: mongodb subdocument


    【解决方案1】:

    下面的查询应该可以工作。由于是嵌入式数组,使用 $gte 时,“points.bonus”和“points.points”应该这样引用。

    db.collection.find({$and:[{"points":{"points":85,"bonus":20}}, {"points.points" : 64, "points.bonus" : {$gte : 10}}]})
    

    在第二个示例中,没有$gte。因此,您将收到两个查询的响应。

    【讨论】:

      猜你喜欢
      • 2015-03-10
      • 1970-01-01
      • 2013-09-12
      • 1970-01-01
      • 2014-09-06
      • 1970-01-01
      • 2013-02-22
      • 2013-09-05
      • 1970-01-01
      相关资源
      最近更新 更多