【发布时间】:2017-04-08 03:32:40
【问题描述】:
在 Mongo 3.4 中
我有一个包含以下格式的文档的集合:
类型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"
}
]
},
]
}
}
}
}
如何编写汇总语句,以便仅从 Portfolio 与某个值匹配的文档中获取所有货币值。
我一直在使用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'}}}}}}]}
【问题讨论】:
标签: mongodb