我有以下收藏:
[
{
"_id": 1,
"favorite": {
"color": "red",
"foods": {
"fruits": "banana",
"fastfood": [
"burger",
"sandwich"
]
}
}
},
{
"_id": 2,
"favorite": {
"color": "green",
"foods": {
"noodles": "ramen",
"fastfood": [
"fries",
"burger",
"corn dog"
]
}
}
},
{
"_id": 3,
"favorite": {
"color": "red",
"foods": {
"soup": "cream soup"
}
}
}
]
我必须进行以下查询:
db.collection.find({
"favorite.foods.fruits": "banana",
"favorite.foods.fastfood": {
"$all": [
"burger"
]
}
})
当前结果:
[
{
"_id": 1,
"favorite": {
"color": "red",
"foods": {
"fastfood": [
"burger",
"sandwich"
],
"fruits": "banana"
}
}
}
]
预期结果: 但这不是我预期的结果。我需要为快餐数组匹配完整的favorite.foods.fastfood对象。如果快餐与我的查询不同,则查询应返回 null/false/empty。
另外,使用 mongoTemplate 的查询结果对我来说也很好。
只需用于
$setEquals
执行数组比较。蒙戈游乐场
如果为了保持相同而顺序很重要,那么只需删除
$all
运算符即可:游乐场演示