我只想在選單中投影“型別”:“A”。有沒有辦法做到這一點?
"_id" : "1",
"menu" : [
{
"type" : "A",
"items" : [
{
"key" : "Add",
"enabled" : true,
} ]
},
{
"type" : "B",
"items" : [
{
"key" : "Add",
"enabled" : true,
} ]
}
]
}
uj5u.com熱心網友回復:
如果您只想輸出型別為 A 的選單,您可以$elemMatch在查找查詢中使用(請注意,因為這只回傳第一個匹配的元素)
db.collection.find({},
{
"menu": {
"$elemMatch": {
"type": "A"
}
}
})
這里的例子
或者$filter在聚合查詢中(這將回傳type: "A"陣列中的所有物件):
db.collection.aggregate([
{
"$project": {
"menu": {
"$filter": {
"input": "$menu",
"cond": {
"$eq": [
"$$this.type",
"A"
]
}
}
}
}
}
])
這里的例子
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/454355.html
標籤:数组 mongodb 猫鼬 mongodb查询 pymongo
上一篇:通過id查找深度嵌套的物件
