mongojs - mongodb update and/or change an array key without using the value -


i'm having trouble removing/renaming array object mongodb.

{      "_id" : objectid("556a7e1b7f0a6a8f27e01b8a"),      "accountid" : "ac654164545",      "sites" :[         { "site_id" : "example1.com" },         { "002" : "example2.com" },         { "003" : "example3.com" },         { "004" : "example4.com" },         { "005" : "example5.com" },         { "006" : "example6.com" }      ]} } 

please take notice of array key "site_id", want change "001" either removing , appending it, know how do, or rename it.

i've tried:

db.accounts.update({'id':objectid("556a7e1b7f0a6a8f27e01b8a")}, {$unset: {sites.site_id}}) 

but says "unexpected token". tried:

db.accounts.update({'id':objectid("556a7e1b7f0a6a8f27e01b8a")}, {$unset: {sites:site_id}}) 

that says "site_id not defined"

then tried:

db.accounts.update({'id':objectid("556a7e1b7f0a6a8f27e01b8a")}, {$unset: {sites:'site_id'}}) 

that says writeresult({ "nmatched" : 0, "nupserted" : 0, "nmodified" : 0 })

i tried $rename command:

db.accounts.update( { _id:objectid("556a7e1b7f0a6a8f27e01b8a") }, { $rename: { "sites.site_id": "sites.001" } } ) 

but gave me "cannot use part (sites of sites.site_id) traverse element"

one option use .find(), iterate through , delete it. save undeleted ones object, , run .insert() command, want stay away if have too.

this site talks dynamic renaming: http://docs.mongodb.org/manual/reference/operator/update/positional/

aka first make matching query , use $ match index in array.

here's query that'll accomplish want test data provided:

db.accounts.update({'accountid':"ac654164545", "sites.site_id": "example1.com"}, {$set: {"sites.$": {'001': 'example1.com'}}}) 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -