Query tangled array in Pymongo

大城市里の小女人 提交于 2019-12-11 16:11:51

问题


I am trying to query a very tangled collection.

The schema:

{'tags': {'variables': [{'value': '3x9', 'var_name': 's'},
                     {'value': '12:00AM', 'var_name': 'x'},
                     {'value': 'goog', 'var_name': 'y'}]},
                     'url': 'https://www.google.com'}]

The Query:

res = mycol.aggregate([{"$unwind":"$tags"}
     ,{'$match': {'tags.tag.name':'A A', 'aR': 98765}}
     ,{'$project': {'url': 1, 'tags.vars': 1, '_id': 0}},])


res = list(res)
for x in res: 
    print(res)

The Result:

{'tags': {'variables': [{'value': '3x9', 'var_name': 's'},
                     {'value': '12:00AM', 'var_name': 'x'},
                     {'value': 'goog', 'var_name': 'y'}]},
                     'url': 'https://www.google.com'}]

The Expected Result:

[{'value': '3x9', 'var_name': 's'},
{'value': '12:00AM', 'var_name': 'x'},
{'value': 'goog', 'var_name': 'y'},
'url': 'https://www.google.com']

I will use it as a HttpResponse and use it as a JSON object.

Please Help!


回答1:


Can you just say res['tags']['variables'] instead of res = list(res)?



来源:https://stackoverflow.com/questions/52829141/query-tangled-array-in-pymongo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!