python - project nested columns in mongodb -
i have collection in mongo data looks this:
{ "myinput": "myinput", "myoutput": { "result": { "crossdata": { "crossdto": [ { "col1": "11", "col2": "12", }, { "col1": "21", "col2": "22", }, { "col1": "31", "col2": "32", } ], "reqpartnumber": "myinput" } }, "status": { "code": "0", "message": "successful operation", "success": "true" } } }
i trying create mapping table in sql data looks crossdto exist if status success:
myinput 11 12 myinput 21 22 myinput 31 32 ...
i trying come mongodb query on query side as can don't end crazy nested structure in python convert table format.
what have done far:
db.collection.find({'myoutput.status.success':'true'}, {_id:0, myoutput:1}).limit(1)
and select records status success , meanwhile, show "myoutput"..
but want few levels deep, this:
{ "crossdto": [ { "col1": "1", "col2": "1", }, { "col1": "2", "col2": "2", }, { "col1": "3", "col2": "3", } ], "reqpartnumber": "myinput" }
can me ? how can configure projection
part of mongodb can nest columns want... expect similar this:
{_id=0, myoutput.result.crossdata=1}
thanks :)
the best way in mongodb use aggregation framework. framework pipeline allows query , transform data. in case, you'll want this:
db.collection.aggregate({ $match: { 'myoutput.status.success':'true' } }, { $project: { 'crossdto': '$myoutput.result.crossdata.crossdto', 'reqpartnumber': '$myoutput.result.crossdata.reqpartnumber' } })
doing in python isn't bad, , how you'll have if have more data aggregation framework can handle:
mydata = [doc['myoutput']['result']['crossdata'] doc in db.collection.find({'myoutput.status.success':'true'}, {_id:0, myoutput:1})]
Comments
Post a Comment