匹配的文件,並得到所有值在蒙戈3.4使用MongoDB的聚集到了現場
我與格式文檔的集合:
類型1:
{
"Level1": {
"@version": "genR",
"@revision": "aux",
"Level2": {
"container": {
"type": "ARRAY",
"categories": [
{
"category": [
{
"Type": "STRING",
"Value": "Currency"
},
{
"Type": "STRING",
"Value": "EUR"
}
]
},
{
"category": [
{
"Type": "STRING",
"Value": "Portfolio"
},
{
"Type": "STRING",
"Value": "ABCDEF"
}
]
},
]
}
}
}
}
類型2:
{
"Level1": {
"@version": "genR",
"@revision": "aux",
"Level2": {
"container": {
"type": "ARRAY",
"categories": [
{
"category": [
{
"Type": "STRING",
"Value": "Currency"
},
{
"Type": "STRING",
"Value": "EUR"
}
]
},
{
"category": [
{
"Type": "STRING",
"Value": "Portfolio"
},
{
"Type": "STRING",
"Value": "ABCDEF"
}
]
},
{
"category": [
{
"Type": "STRING",
"Value": "Short Description"
},
{
"Type": "STRING",
"Value": "Cash Only"
}
]
},
]
}
}
}
}
我該如何編寫一個彙總語句,以便我可以從文檔中獲得所有貨幣值在哪裏投資組合匹配一定的價值。
我一直用如下pymongo的總框架:
pipeline = [{"$unwind":"$Level1.Level2.container.categories"},{"$unwind":"$Level1.Level2.container.categories.category"},{"$match":{"Level1.Level2.container.categories.category.Value":"Portfolio"}}]
pprint(db.command('aggregate',collection,pipeline=pipeline))
但沒有結果。 Pymongo有點混亂。即使有人可以指出一般的方法,它確實會有所幫助。
預期的響應假設4個匹配文件(分別設定不同的類別項目數量):
{'Currency': [{'Level1': {'Level2': {'container': {'categories': {'category': {'Value': 'EUR'}}}}}},
{'Level1': {'Level2': {'container': {'categories': {'category': {'Value': 'EUR'}}}}}},
{'Level1': {'Level2': {'container': {'categories': {'category': {'Value': 'USD'}}}}}},
{'Level1': {'Level2': {'container': {'categories': {'category': {'Value': 'EUR'}}}}}}]}
這是給我所有的類別,除了投資組合。但是你指出了我的正確方向。我會盡快更新評論。謝謝 – sunny
Np。請添加預期的json響應,並相應地調整查詢。 – Veeram
我還有其他文檔具有比'Portfolio'和'Currency'更多的'category'字段。此類別子結構基於文檔類型是動態的。 – sunny