python - How to add data to a (heavily) nested collection in MongoDB -


i noob @ python , mongodb , appreciate problem. collection in mongodb looks this:

{  "segments" : [                 {                   devices : [                               "ip" : "",                               "interfaces" :                                               [                                                {                                                  "name" :""                                                }                                              ],                              ],                              "devicename" : "",                              "segmentname" : ""                  }                 ] } 

i have object so:

node details: {'node:98a': ['sweden', 'stockholm', '98a-3470'], 'node:98b': ['denmark', 'copenhagen', '98b-3471', '98b-3472']} 

i need update 'name' within 'interfaces' part in collection above, values node details dictionary. have tried using $set, $addtoset, $push etc., nothing helping. have added segment , devicename information.

the output should follows:

{      "segments" : [                     {                       devices : [                                   {                                    "interfaces" :                                                   [                                                    {                                                      "name" :"98a-3470"                                                    }                                                  ],                                    "devicename" : "node:98a",                                   }                                   {                                    "interfaces" :                                                   [                                                    {                                                      "name" :"98b-3471"                                                    },                                                    {                                                      "name" :"98b-3472"                                                    }                                                  ],                                    "devicename" : "node:98b",                                   }                                  ],                                                        "segmentname" : "segmenta"                      }                     ]     } 

any appreciated. have tried lot in mongodb shell , on google, no avail. thank all.

regards, trupsster

[[ edited ]] okay, here have got far after continuing poke around after posing question: used following query in mongodb shell:

db.test.mycoll.update({'segments.segmentname':'segmenta','segments.devices.name':'node:98a'}, {$set: {"segments.$.devices.0.interfaces.name" : "98b-3470"}}) 

now inserted in correct place per 'schema', when try add second interface, replaces earlier one. tried using $push (complained not being array), , $addtoset (showed error), none helped. can please me point on?

thanks, trupsster

[[ edited again ]]

i found solution! here did: add interface existing device:

db.test.mycoll.update({'segments.segmentname':'segmenta','segments.devices.name':'node:98a'}, {$addtoset: {"segments.$.devices.0.interfaces.name" : "98a-3471"}}) 

now, append dict new 'name' within array 'interfaces':

db.test.mycoll.update({'segments.segmentname':'segmenta','segments.devices.name':'node:98a'}, {$addtoset: {"segments.$.devices.0.interfaces" : {"name" : "98a-3472"}}}) 

as can see, used $addtoset.

now, next step add same information (with different values) 2nd device, done so:

db.test.mycoll.update({'segments.segmentname':'segmenta','segments.devices.name':'node:98b'}, {$addtoset: {"segments.$.devices.1.interfaces" : {"name" : "98b-3473"}}}) 

so it! chuffed myself! thank took time read problem. hope solution someone.

regards, trupsster

you did not tried. access sub-document inside array, need use dot notation numeric indices. address name field in example:

segments.devices.0.interfaces.0.name 

did try that? work?


Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -