在 Mongo 子文档中查询多个字段
In Mongo query in Subdocuments multiple fields
我在Mongo
中有一个collection
{
"_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}})
它 returns id 为 1 和 2 的文档。
现在,如果我想检索具有(points=85 和 bonus = 20) 和另一个具有 {points=85 和 bonus > 10)的 sub-documents 的元素。基本上我想检索 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".
提前致谢。
抱歉语法错误。
下面的查询应该有效。由于是内嵌数组,"points.bonus"和"points.points"在使用$gte
时应该这样引用。
db.collection.find({$and:[{"points":{"points":85,"bonus":20}}, {"points.points" : 64, "points.bonus" : {$gte : 10}}]})
第二个例子中没有$gte
。因此,您将获得对这两个查询的响应。
我在Mongo
中有一个collection{
"_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}})
它 returns id 为 1 和 2 的文档。
现在,如果我想检索具有(points=85 和 bonus = 20) 和另一个具有 {points=85 和 bonus > 10)的 sub-documents 的元素。基本上我想检索 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".
提前致谢。 抱歉语法错误。
下面的查询应该有效。由于是内嵌数组,"points.bonus"和"points.points"在使用$gte
时应该这样引用。
db.collection.find({$and:[{"points":{"points":85,"bonus":20}}, {"points.points" : 64, "points.bonus" : {$gte : 10}}]})
第二个例子中没有$gte
。因此,您将获得对这两个查询的响应。