100% found this document useful (1 vote)
92 views1 page

MongoDB Update One or More Fields of One or All Documents

The document summarizes how to update documents in MongoDB. It describes replacing an entire document by identifying it with its ID or another field. It also explains how to update specific fields of a single document using the $set operator to replace field values or the $inc operator to increment numeric fields. Finally, it discusses updating fields of multiple documents by not specifying the document identifier.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
92 views1 page

MongoDB Update One or More Fields of One or All Documents

The document summarizes how to update documents in MongoDB. It describes replacing an entire document by identifying it with its ID or another field. It also explains how to update specific fields of a single document using the $set operator to replace field values or the $inc operator to increment numeric fields. Finally, it discusses updating fields of multiple documents by not specifying the document identifier.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

specify.

io

The MongoDB Update Command

Table of Content
1.1    Replace an existing document entirely

1.2    Update one or more elds of a single document

1.2.1  Replace document elds using the $set operator


   "MongoDB update one field" is explained here ⤴

1.2.2  Increment numeric elds using the $inc operator

1.3    Update one or more elds of multiple documents


   "MongoDB update one field" is explained here ⤴

Introduction

This article shows how to update one or multiple existing documents in


a MongoDB database.

The following instructions assumes that there is a collection


"products" which contains documents with the same structure like this
one:

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"
}
}

In general, the MongoDB update command looks like this:

The MongoDB update


db.collection-name.update(document selector, updated data, [update options]) command syntax

The collection name speci es the collection in which the


document(s) to update is (are) located.
The document selector describes which document(s) from the
collection should be updated. The same query statements as in the
MongoDB nd function can be used.
The updated data describes how the document(s) should be
modi ed.

Replace an existing document entirely

The simplest use case of the MongoDB update command is to replace


a single existing document entirely.

For example, if we want to replace the above example document


representing a customer we can execute the following command in a
MongoDB client:

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"
}
}
);

In this update command the internal object ID is used as document


selector to identify the document which should be replaced.

The second parameter of the update statement above is the new


document that will override the old one.

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.

As a result of the update command you should see the following


output indicating that one document has been updated.

The result of the


WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) previous MongoDB
update command if
the operation
succeeded.
If you can't or don't want to identify the document which you would
like to replace with the internal ID you can also use any other eld. The
following update statement will replace the document which current
rstname eld is "Max".

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.

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

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 speci c elds of a single document

Instead of replacing the whole document it is mostly required to just


modify one or some elds of a document and keep the other elds
untouched. For this use case MongoDB provides di erent operators
that can be speci ed in the update command.

Replace eld values of a document using the $set operator

If only one eld of a document should be updated the "$set" operator


can be used. The set operator will override the old value of a speci ed
eld.

The following update command nds the rst document which


rstname eld is equal "Max" and updates (sets) the lastname eld to
"Maier".

Update a single field


> db.customers.update( of a single document
{"firstname": "Max"},
{
$set: {
"lastname": "Maier"
}
}
);

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" .

To update multiple elds of a document the command looks like this:

Update a multipe
> db.customers.update( fields of a single
{"firstname": "Max"}, document
{
$set: {
"lastname": "Maier",
"email": "p.maier@example.com"
}
}
);

To update a eld that is nested in the JSON structure the following


command can be used:

Update a nested
> db.customers.update( fields of a single
{"firstname": "Max"}, document
{
$set: {
"lastname": "Maier",
"address.street": "another street"
}
}
);

Increment numeric eld values of a document using the


$inc operator

For numeric eld values it is possible to modify the value regarding


their old value. The MongoDB $inc operator is intended to be used to
ful l this purpose. The following example will increase the zip code of a
customer by one. When the zip code is 94101 it will be 94102 after the
update.

Increasing a numeric
> db.customers.update( value of a document.
{_id: ObjectId("57dcf6af1eb6b4f388a7add5")},
{$inc: {"address.zip": 1 }}
);

Decreasing values is also possible with the $inc operator. To do this


just specify a negative value. The following command will decrease the
value by 5.

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" .

Update speci c elds of multiple documents

So far all MongoDB update queries only updated/replaced exactly one


document even though the selector matched multiple documents. To
update all matching documents an addional option must be set:

Update a field of all


> db.customers.update( matching documents
{"firstname": "Max"},
{
$set: {
"email": "p.maier@example.com"
}
},
{"multi": true}
);

This command returns the following result:

WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 1 })

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:

MongoDB Introduction - MongoDB allows to store and nd JSON- Features


documents.

Find documents in MongoDB using the mongo shell How-to

Install and start a MongoDB server How-to

Home | Imprint

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy