mongodb - Rename/update a specific tag in all documents -


i have these documents in collection:

[{     "_id" : objectid("57496afadc964de30a8084a1"),     "name" : "mongodb: definitive guide",     "tags" : [ "it", "sql" ] },{     "_id" : objectid("57496afadc964de30a8084a2"),     "name" : "mongodb applied design patterns",     "tags" : [ "sql", "mongodb" ] }] 

i need rename "sql" tags "nosql" in documents. can first adding new tag-name, , removing 1 needs updating:

  db.getcollection('book').update(     { tags: 'sql' },     { '$push': { tags: 'nosql' } },     { multi: true })    db.getcollection('book').update(     { tags: 'sql' },     { '$pull': { tags: 'sql' } },     { multi: true }) 

but requires 2 separate queries.

how can achieve singe statement?

you using wrong operators. need use $set operator , $ positional update operator. should using updatemany() because update() deprecated in official language drivers.

db.getcollection('book').updatemany(     { 'tags': 'sql' },      { '$set': { 'tags.$': 'nosql' } } ) 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -