Random MongoDb Syntax: Updates
- by Liam McLennan
I have a MongoDb collection called tweets. Each document has a property system_classification. If the value of system_classification is ‘+’ I want to change it to ‘positive’. For a regular relational database the query would be: update tweets
set system_classification = 'positive'
where system_classification = '+'
the MongoDb equivalent is:
db.tweets.update({system_classification: '+'},
{$set: {system_classification:'positive'}}, false, true)
Parameter
Description
{ system_classification: '+' }
the first parameter identifies the documents to select
{ $set: { system_classification: 'positive' } }
the second parameter is an operation ($set) and the parameter to that operation {system_classification: ‘positive’}
false
the third parameter indicates if this is a regular update or an upsert (true for upsert)
true
the final parameter indicates if the operation should be applied to all selected documents (or just the first)