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
Post a Comment