Created
March 7, 2011 06:15
-
-
Save sym3tri/858142 to your computer and use it in GitHub Desktop.
How to update a single field in a MongoDB collection for all documents matching a specific criteria
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// FROM: http://www.mongodb.org/display/DOCS/Updating#Updating-update%28%29 | |
// | |
// db.collection.update( criteria, objNew, upsert, multi ) | |
// criteria - query which selects the record to update; | |
// objNew - updated object or $ operators (e.g., $inc) which manipulate the object | |
// upsert - if this should be an "upsert"; that is, if the record does not exist, insert it | |
// multi - if all documents matching criteria should be updated | |
// | |
// SQL VERSION: | |
// UPDATE myTable SET dateField = '2011-01-01' WHERE condField = 'condValue' | |
db.myCollection.update({condField: 'condValue'}, { $set: { dateField: new Date(2011, 0, 1)}}, false, true); | |
Works! The 4th parameter was what I was missing.
The following query doesn't work, when I want to update subfield (which is an int) of one of the parent fields of all documents in a collection:
db.myCollection.update( {}, { $set: { 'rootField.toBeModifiedField': 1}}, false, true);
what am i missing?
Thanks!
brother try it:
db.myCollection.update( {}, { $set: { rootFieldto:{BeModifiedField: 1}}}, false, true);
Does anyone know if it's possible to update multiple fields in the one query? I.e
return this.update({"Name": {$regex: regex}},
{ $set: { 'keyToUpdate': param1 , 'otherKeyToUpdate': param2 } }, {multi: true})
.execAsync();
Thank you, works!
Thank you!
Thanks!
Life's problems just decreased by one because of you. Thank you.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yay. Thank you. :)