MongoDB Update One or More Fields of One or All Documents
MongoDB Update One or More Fields of One or All Documents
io
Table of Content
1.1 Replace an existing document entirely
Introduction
An example JSON
{ document that will
"_id" : ObjectId("57cc58333d496dc219c09c2c"), be used in the
"firstname" : "Max", MongoDB update
"lastname" : "Mustermann", commands below.
"email" : "m.mustermann@example.com",
"password" : "d9729feb74992cc3482b350163a1a010",
"last_login" : "2015-01-07",
"note" : "Always pays in time, very good customer!",
"address" :
{
"country" : "Germany",
"street" : "Beispielstrasse 64",
"zip" : "62717"
}
}
Identify a document
> db.customers.update( by its internal ID
{"_id": ObjectId("57cc58333d496dc219c09c2c")}, and replace it
entirely.
{
"firstname" : "Peter",
"lastname" : "Mustermann",
"email" : "p.mustermann@example.com",
"password" : "d9729feb74992cc3482b350163a1a010",
"last_login" : "2015-01-07",
"note" : "Always pays in time, very good customer!",
"address" :
{
"country" : "Germany",
"street" : "Beispielstrasse 64",
"zip" : "62717"
}
}
);
Note: Although only the rst name and the email elds changes in the
customer document, each eld must be speci ed in this update command
because the whole document is replaced. Values that are not speci ed in
this update command will not be available in the document anymore. Only
the '_id' eld must not be speci ed since it's an internal eld maintained by
MongoDB.
Identify a document
> db.customers.update( by another field then
{"firstname": "Max"}, its ID and replace it
entirely.
{
"firstname" : "Peter",
"lastname" : "Mustermann",
"email" : "p.mustermann@example.com",
"password" : "d9729feb74992cc3482b350163a1a010",
"last_login" : "2015-01-07",
"note" : "Always pays in time, very good customer!",
"address" :
{
"country" : "Germany",
"street" : "Beispielstrasse 64",
"zip" : "62717"
}
}
);
The result of this update looks the same like the result of the previous
update.
Note: Even if there is more then one document which rstname eld is
"Max" only the rst document will be updated. The other matching
documents won't be modi ed. For more information how to update all
matching documents see chapter "Update elds of multiple documents" .
Note: Even if there is more then one document which rstname eld is
"Max" only the rst document will be updated. The other matching
documents won't be modi ed. For more information how to update all
matching documents see chapter "Update elds of multiple documents" .
Update a multipe
> db.customers.update( fields of a single
{"firstname": "Max"}, document
{
$set: {
"lastname": "Maier",
"email": "p.maier@example.com"
}
}
);
Update a nested
> db.customers.update( fields of a single
{"firstname": "Max"}, document
{
$set: {
"lastname": "Maier",
"address.street": "another street"
}
}
);
Increasing a numeric
> db.customers.update( value of a document.
{_id: ObjectId("57dcf6af1eb6b4f388a7add5")},
{$inc: {"address.zip": 1 }}
);
Decreasing a numeric
> db.customers.update( value of a document.
{_id: ObjectId("57dcf6af1eb6b4f388a7add5")},
{$inc: {"address.zip": -5 }}
);
Note: Even if there is more then one document that matches the query
selector only the rst document will be updated. The other matching
documents won't be modi ed. For more information how to update all
matching documents see chapter "Update elds of multiple documents" .
In this example nMatched shows that two documents have been found
by the document selector query but nModi ed shows that only one
document have been updated by the update command. nModi ed is
smaller then nMatched in case the update command sets the elds of
some document to its current values. In other words: when the update
operation doesn't change any eld of some documents.
Related Resources:
Home | Imprint