aggregation framework - mongoDb: aggregate document references to subgroups -
i've set of bson documents in mongodb collection want process according form cascading sub-documents. there thousand of duplicate id's differentiated timestamps , documents type subsystem-> component -> subcomponent; , should attached each other based on source. sample have:
[{ "id": 1001, "type": "subsystem", "time": 1454767739, "payload": { "key": "..." } }, { "id": 1001, "type": "subsystem", "time": 1454767539, "payload": { "key": "..." } }, { "id": 2001, "type": "component", "time": 1454767778, "source": 1001, "payload": { "key": "..." } }, { "id": 5002, "type": "subcomponent", "time": 1454767779, "source": 2001, "payload": { "key": "..." } }, { "id": 5003, "type": "subcomponent", "time": 1454767798, "source": 2001, "payload": { "key": "..." } }] and trying this:
- match elements timestamp lower
maxtimevalue - group elements (unique) id, use newest value (
$max: $time)$$root(everyidunique now) - group attribute
sourcearray elementid(or_id)
i've achieved 1.) , 2.) have group:
[{ "_id": 1001, "reftime": 1454767739, "element": [ { "id": 1001, "type": "subsystem", "time": 1454767739, "payload": { "key": "..." } } ] }, { "_id": 2001, "reftime": 1454767778, "element": [ { "id": 2001, "type": "component", "time": 1454767778, "source": 1001, "payload": { "key": "..." } } ] }, { "_id": 5002, "reftime": 1454767779, "element": [ { "id": 5002, "type": "subcomponent", "time": 1454767779, "source": 2001, "payload": { "key": "..." } } ] }, { "_id": 5003, "reftime": 1454767798, "element": [ { "id": 5003, "type": "subcomponent", "time": 1454767798, "source": 2001, "payload": { "key": "..." } } ] }] but want elements structured according links in source value. this:
{ "_id": 1001, "reftime": 1454767739, "element": [ { "id": 1001, "type": "subsystem", "time": 1454767739, "payload": { "key": "..." } } ], "attached": [ { "_id": 2001, "reftime": 1454767778, "element": [ { "id": 2001, "type": "component", "time": 1454767778, "source": 1001, "payload": { "key": "..." } } ], "attached": [ { "_id": 5002, "reftime": 1454767779, "element": [ { "id": 5002, "type": "subcomponent", "time": 1454767779, "source": 2001, "payload": { "key": "..." } } ] }, { "_id": 5003, "reftime": 1454767798, "element": [ { "id": 5003, "type": "subcomponent", "time": 1454767798, "source": 2001, "payload": { "key": "..." } } ] } ] } ] } is possible? i'm struggling form group has source , combine group sample 2.). or if help, introduce source: 0? every subsystem , group under single root element?
any ideas or insights? i'm stuck @ point. thank you!
Comments
Post a Comment