現在我有當前的檔案:
[
{
"name": "John",
"state": "CA",
"people": [
{
"id": "1",
"qty": "5",
"type": "3"
},
{
"id": "4",
"qty": "5",
"type": "6"
}
]
},
{
"name": "Katie",
"state": "NY",
"people": [
{
"id": "434",
"qty": "5",
"type": "63"
},
{
"id": "34",
"qty": "6",
"type": "21"
}
]
}
]
我想要的是一個查詢,它只檢索名稱、id 和 qty,在 id 上有一些查詢條件(這里是 qty = 5),格式如下(不包括父檔案“people”):
[{"name": "John", "id": "1", "qty": "5",},
{"name": "John", "id": "4", "qty": "5",},
{"name": "Katie", "id": "434", "qty": "5",}]
我有這個查詢:
db.collection.find( { "qty": "5"}, { "name": 1,"people.id": 1, "people.qty": 1});
但這會回傳父檔案,即給我這個:
{ name: 'John',
people: { id: '1', qty: '5'} }
任何想法如何查詢以便我獲得所需的輸出?
uj5u.com熱心網友回復:
詢問
- uwind 人們在不同的檔案中獲取每個成員
- 過濾只得到
qty=5 - 專案取消嵌套并獲得您想要的結構
*如果您有一個多鍵索引,people.qty您也可以將其添加為第一階段
{"$match": {"people.qty": "5"}}
測驗代碼在這里
aggregate(
[{"$unwind": {"path": "$people"}},
{"$match": {"people.qty": "5"}},
{"$project":
{"_id": 0, "name": 1, "id": "$people.id", "qty": "$people.qty"}}])
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/362975.html
上一篇:動態鏈接查詢
