“update”
method of collection is used to update a document.
Syntax
db.collection.update(query,
update, options)
“update”
method is used to update an existing document (or) documents in the collection.
Depending on options in the query, update method update/replace the document.
Detailed
syntax looks like below.
db.collection.update(
<query>,
<update>,
{
upsert: <boolean>,
multi: <boolean>,
writeConcern: <document>
}
)
Parameter
|
Type
|
Description
|
query
|
document
|
The
selection criteria for the update.
|
update
|
document
|
The
modifications to apply.
|
upsert
|
boolean
|
Optional.
If set to true, creates a new document when no document matches the query
criteria. The default value is false, which does not insert a new document
when no match is found.
|
multi
|
boolean
|
Optional.
If set to true, updates
multiple documents that meet the query criteria. If set to false, updates one document. The default value is false.
|
writeConcern
|
document
|
Optional.
A document expressing the write concern.
|
> db.employee.find() { "_id" : 1, "firstName" : "Joel", "lastName" : "chelli", "salary" : 25000, "hobbies" : [ "watching movies", "playing games" ] } { "_id" : 2, "firstName" : "Ananad", "lastName" : "Bandaru", "salary" : 200000, "hobbies" : [ "car drivinig", "watching movies" ] } { "_id" : 3, "firstName" : "Gopi", "lastName" : "Battu", "salary" : 65000, "hobbies" : [ "climbing hills", "dancing", "singing" ] } { "_id" : 4, "firstName" : "Ritwik", "lastName" : "Mohenthy", "salary" : 50000, "hobbies" : [ "watching movies", "watching movies", "watching movies" ] } { "_id" : 5, "firstName" : "RamaKrishna", "lastName" : "Gurram", "salary" : 150000, "hobbies" : [ "hackning sites", "Reading books", "listening music" ] } { "_id" : 6, "firstName" : "BalaKrishna", "lastName" : "Gurram", "salary" : 80000, "hobbies" : [ "roaming around", "eating food" ] } { "_id" : ObjectId("54ba2de1f1c8f2149c000740"), "name" : "Krishna", "project" : "A350" } { "_id" : ObjectId("54ba2dfcf1c8f2149c000741"), "name" : "Krishna", "project" : 4 } { "_id" : ObjectId("54ba47c9f1c8f2149c000742"), "name" : "Keerthi", "hobbies" : [ "watching movies", "playing games", "cooking" ] } > > > db.employee.update({"firstName" : "Joel"}, {"firstName" : "Jessi"}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > > db.employee.find() { "_id" : 1, "firstName" : "Jessi" } { "_id" : 2, "firstName" : "Ananad", "lastName" : "Bandaru", "salary" : 200000, "hobbies" : [ "car drivinig", "watching movies" ] } { "_id" : 3, "firstName" : "Gopi", "lastName" : "Battu", "salary" : 65000, "hobbies" : [ "climbing hills", "dancing", "singing" ] } { "_id" : 4, "firstName" : "Ritwik", "lastName" : "Mohenthy", "salary" : 50000, "hobbies" : [ "watching movies", "watching movies", "watching movies" ] } { "_id" : 5, "firstName" : "RamaKrishna", "lastName" : "Gurram", "salary" : 150000, "hobbies" : [ "hackning sites", "Reading books", "listening music" ] } { "_id" : 6, "firstName" : "BalaKrishna", "lastName" : "Gurram", "salary" : 80000, "hobbies" : [ "roaming around", "eating food" ] } { "_id" : ObjectId("54ba2de1f1c8f2149c000740"), "name" : "Krishna", "project" : "A350" } { "_id" : ObjectId("54ba2dfcf1c8f2149c000741"), "name" : "Krishna", "project" : 4 } { "_id" : ObjectId("54ba47c9f1c8f2149c000742"), "name" : "Keerthi", "hobbies" : [ "watching movies", "playing games", "cooking" ] }
As
you observe “db.employee.update({"firstName" : "Joel"},
{"firstName" : "Jessi"})” method update document where
“firstName” is “Joel”. Closely observe the result, actually it replaced entire
document. The fields like “lastName”, “salary”, “hobbies” for the document 1
(_id=1) are no more. It is because, update method replace document with
completely new document.
I
will explain how to update specific fields in next post.
No comments:
Post a Comment