在Mongo查询Subdocuments中的多个字段

时间:2016-10-11 09:52:21

标签: mongodb subdocument

我在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的文档。

现在,如果我想要检索具有(points = 85和bonus = 20)的元素以及{points = 85和bonus>的其他子文档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 “$和”

提前致谢。 对不起语法错误。

1 个答案:

答案 0 :(得分:0)

以下查询应该有效。因为它是一个嵌入式数组,所以当使用 $gte 时,应该像这样引用“points.bonus”和“points.points”。

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

在第二个示例中,没有$gte。所以,你得到了两个查询的回复。