Query tangled array in Pymongo

2024/10/12 10:20:01

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!

Answer

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

https://en.xdnf.cn/q/118210.html

Related Q&A

manipulating value of pandas dataframe cell based on value in previous row without iteration

I have a pandas dataframe with~3900 rows and 6 columns compiled from Google Finance . One of these columns defines a time in unix format, specifically defining a time during the trading day for a marke…

Convert ctypes code to cython

Id like to convert some ctypes code to use cython instead, but Im struggling. Essentially, the ctypes code:copies the contents (floats) of two lists into C-compatible structs sends the structs to my b…

Enable PyROOT Ubuntu 14.04

I downloaded madpgraph5, but when I run it I get the following error:ERROR: ROOT file called ROOT.py or ROOT.pyc is not foundERROR: Please check that ROOT is properly installed.When I try locate ROOT.p…

pygal on windows - cannot access classes from pygal

I have such short script:import pygal if __name__ == __main__:bar_chart = pygal.Bar()and following error: AttributeError: module object has no attribute BarDo you have any idea what is wrong? Shall I …

Parsing table for a link

Ive been able to isolate a row in a html table using Beautiful Soup in Python 2.7. Been a learning experience, but happy to get that far. Unfortunately Im a bit stuck on this next bit.I need to get t…

build matrix from blocks

I have an object which is described by two quantities, A and B (in real case they can be more than two). Objects are correlated depending on the value of A and B. In particular I know the correlation m…

How to convert string with UTC offset

I have date as In [1]: a = "Sun 10 May 2015 13:34:36 -0700"When I try to convert it using strptime, its giving error.In [3]: datetime.strptime(a, "%a %d %b %Y %H:%M:%S %Z"...: ) ---…

regex multiline not working on repeated patterns

I am trying to get a regex expression to match multiple patterns with multilines but it keeps matching everything. For instance I want to match two times this code:STDMETHOD(MyFunc)( D2D1_SIZE_U size, …

Django Rest Framework slug_field error

I have this serializer and model. I want to show in my API the field username of User model, but I receive this error.AttributeError at /api/v1/client_share_requests/1/Profile object has no attribute u…

Django - 500 internal server error no module named django

django return 500 internal server error (apache 2.4.10, ubuntu 15.04, django 1.9.6)apache log:[wsgi:warn] mod_wsgi: Compiled for Python/3.4.2. [wsgi:warn] mod_wsgi: Runtime using Python/3.4.3. [mpm_eve…