- Reference >
mongo
Shell Methods >- Collection Methods >
- db.collection.save()
db.collection.save()¶
On this page
Definition¶
-
db.collection.
save
(document)¶ Updates an existing document or inserts a new document, depending on its
document
parameter.The
save()
method takes the following parameter:Parameter Type Description document
document A document to save to the collection. If the
document
does not contain an _id field, then thesave()
method performs an insert. During the operation, the shell will create an ObjectId and assign it to the_id
field.If the
document
contains an _id field, then thesave()
method performs anupsert
, querying the collection on the_id
field. If a document does not exist with the specified_id
value, thesave()
method performs an insert. If a document exists with the specified_id
value, thesave()
method performs an update that replaces all fields in the existing document with the fields from thedocument
.
Examples¶
The following are examples of the save()
method. The following examples show how to use the
save()
method to insert or update a document
into either the products
collection or the bios
collection.
Save a New Document without Specifying an _id
Field¶
In the following examples, the parameter to the
save()
method is a document without an _id
field. This means the save()
method performs
an insert. During the insert, mongod
will create the _id
field with a unique ObjectId value.
The ObjectId
values are specific to the machine and time when the
operation is run. As such, your values may differ from those in the
example.
products
Collection¶
This example inserts into the products
collection a document with
the item
field set to book
and qty
field set to 40
:
The inserted document includes an _id
field with the generated
ObjectId
value:
bios
Collection¶
This example inserts a new document into the The bios Example Collection:
The inserted document includes an _id
field with the generated
ObjectId
value.
Note
Most MongoDB driver clients will include the _id
field and
generate an ObjectId
before sending the insert operation to
MongoDB; however, if the client sends a document without an _id
field, the mongod
will add the _id
field and generate
the ObjectId
.
Save a New Document Specifying an _id
Field¶
The following examples pass the save()
method
a document with an _id
field. Because the _id
field holds a
value that does not exist in the collection, the operations insert
new documents. The results of these operations are identical to an
update operation with the upsert flag set
to true
or 1
.
products
Collection¶
This example creates a new document with the _id
field set to
100
, the item
field set to water
, and the qty
field set
to 30
:
The operation results in the following new document in the products
collection:
bios
Collection¶
This example creates a new document in the
The bios Example Collection. The
save()
method inserts the document because the
bios
collection has no document with the _id
field equal to
10
:
Replace an Existing Document¶
The following example passes the save()
method
a document with an _id
field. Because the _id
field holds a
value that does exist in the collection, the operation performs an
update to replace the existing document.
products
Collection¶
This example replaces the fields and values of the following document
that currently exists in the products
collection:
To replace the document’s data, pass the
save()
method a document that contains the
_id
field set to 100
and the new fields and values:
The operation replaces the existing document with the following: