6
6
7
7
Of the four basic database operations (i.e. CRUD), *create* operations
8
8
are those that add new records or :term:`documents <document>` to a
9
- :term:`collection` in MongoDB. For more information about document
10
- creation and other write operations see :ref:`/core/write-operations`,
11
- for documentation of other basic database operations see the
12
- :doc:`/crud` page.
9
+ :term:`collection` in MongoDB. For general information related to
10
+ document creation and other write operations, see
11
+ :ref:`/core/write-operations`; for documentation of the other CRUD
12
+ operations, see the :doc:`/crud` page.
13
13
14
14
Overview
15
15
--------
@@ -23,15 +23,15 @@ following basic operations.
23
23
24
24
- :ref:`save <crud-create-save>`
25
25
26
- All insert operations in MongoDB the following properties:
26
+ All insert operations in MongoDB exhibit the following properties:
27
27
28
- - If you attempt to insert a document without :term:`_id` field, the
29
- client library *or* the :program:`mongod` instance will add an
30
- ``_id`` an populate the field with a unique :term:`ObjectId
28
+ - If you attempt to insert a document without the :term:`_id` field,
29
+ the client library *or* the :program:`mongod` instance will add an
30
+ ``_id`` field and populate the field with a unique :term:`ObjectId
31
31
<objectid>`.
32
32
33
33
- If you specify an ``_id`` field, the ``_id`` field must be unique
34
- within the collection, otherwise the :program:`mongod` will return a
34
+ within the collection; otherwise the :program:`mongod` will return a
35
35
duplicate key exception.
36
36
37
37
- .. include:: /includes/fact-document-max-size.rst
@@ -55,17 +55,19 @@ the following syntax:
55
55
56
56
db.collection.insert( <document> )
57
57
58
- The :method:`insert() <db.collection.insert()>` method is analogous to
59
- the ``INSERT`` statement in SQL. To illustrate the behavior of
60
- :method:`insert() <db.collection.insert()>`, consider the following
61
- examples:
58
+ .. admonition:: Corresponding Operation in SQL
59
+
60
+ The :method:`insert() <db.collection.insert()>` method is analogous
61
+ to the ``INSERT`` statement.
62
+
63
+ Consider the following examples that illustrate the behavior of
64
+ :method:`insert() <db.collection.insert()>`:
62
65
63
66
- If the collection does not exist, then the :method:`insert()
64
67
<db.collection.insert()>` method creates the collection during the
65
- first insert.
66
-
67
- If the collection ``csbios`` does not exist, then following
68
- operation will create this collection:
68
+ first insert. Specifically in the example, if the collection
69
+ ``csbios`` does not exist, then the insert operation will create this
70
+ collection:
69
71
70
72
.. code-block:: javascript
71
73
@@ -129,10 +131,6 @@ examples:
129
131
:method:`insert() <db.collection.insert()>` method adds the ``_id``
130
132
field to the document and generates a unique ``ObjectId`` for the value.
131
133
132
- The following operation adds the ``_id`` field to the document,
133
- assigns to the field a unique ``ObjectId``, and inserts the document
134
- into the ``csbios`` collection:
135
-
136
134
.. code-block:: javascript
137
135
138
136
db.csbios.insert( {
@@ -186,7 +184,7 @@ examples:
186
184
}
187
185
188
186
- If you pass an array of documents to the :method:`insert()
189
- <db.collection.insert()>` method the :method:`insert()
187
+ <db.collection.insert()>` method, the :method:`insert()
190
188
<db.collection.insert()>` performs a bulk insert into a collection.
191
189
192
190
The following operation inserts three documents into the
@@ -268,15 +266,20 @@ examples:
268
266
Update with Upsert
269
267
------------------
270
268
271
- Typically update operations :doc:`update </applications/update>`
272
- existing documents, but in MongoDB, the :method:`update()
273
- <db.collection.update()>` operation has an ``upsert`` option. Upserts
274
- are a hybrid operation, that use the query argument to the update
275
- operation and:
269
+ An update with upsert or an *upsert* eliminates the need to perform a
270
+ separate database call to check for the existence of a record before
271
+ performing either an update or an insert operation. Typically update
272
+ operations :doc:`update </applications/update>` existing documents, but
273
+ in MongoDB, the :method:`update() <db.collection.update()>` operation
274
+ can accept an ``upsert`` option as an argument. Upserts are a hybrid
275
+ operation that use the ``query`` argument to determine the write
276
+ operation:
276
277
277
- - update the matching document if the query returns a result.
278
+ - If the query returns a result, the upsert updates the matching
279
+ document.
278
280
279
- - inserts a single document if no document matches the query.
281
+ - If the query matches no document in the collection, the upsert
282
+ inserts a single document.
280
283
281
284
Consider the following syntax for an upsert operation:
282
285
@@ -286,7 +289,7 @@ Consider the following syntax for an upsert operation:
286
289
<update>,
287
290
{ upsert: true } )
288
291
289
- Consider the following examples that illustrate the use of the
292
+ The following examples illustrate the use of the
290
293
upsert to perform create operations:
291
294
292
295
- If the ``<update>`` argument contains only field and value
@@ -472,12 +475,16 @@ upsert to perform create operations:
472
475
Save
473
476
----
474
477
475
- The :method:`save() <db.collection.save()>` method inserts a document
476
- into a collection if the document does not contain the ``_id`` field or
477
- contains an ``_id`` field with a value not in the collection. If the
478
- document does not contain the ``_id`` field, the :method:`save()
479
- <db.collection.save()>` method adds the ``_id`` field to the document
480
- and assigns a unique ``ObjectId`` as its value.
478
+ The :method:`save() <db.collection.save()>` method is a specialized
479
+ upsert that use the ``_id`` field in document argument to determine
480
+ whether to perform an insert or an update:
481
+
482
+ - If the document does not contain the ``_id`` field or contains an
483
+ ``_id`` field with a value not in the collection, the :method:`save()
484
+ <db.collection.save()>` method performs an insert of the document.
485
+
486
+ - Otherwise, the :method:`save() <db.collection.save()>` method
487
+ performs an update.
481
488
482
489
The :method:`save() <db.collection.save()>` method has the
483
490
following syntax:
@@ -487,12 +494,12 @@ following syntax:
487
494
db.collection.save( <document> )
488
495
489
496
Consider the following examples that illustrate the use of the
490
- :method:`insert() <db.collection.insert()>` method for create
491
- operations:
497
+ :method:`save() <db.collection.save()>` method to perform inserts:
492
498
493
499
- If the ``document`` does not contain the ``_id`` field, the
494
- :method:`save() <db.collection.save()>` method adds the ``_id`` field
495
- to the document and performs an insert.
500
+ :method:`save() <db.collection.save()>` method performs an insert.
501
+ Refer to the :ref:`insert <crud-create-insert>` section for details
502
+ of the insert operation of a document without an ``_id`` field.
496
503
497
504
The following operation performs an insert into the ``csbios``
498
505
collection since the document does not contain the ``_id`` field:
@@ -513,36 +520,10 @@ operations:
513
520
]
514
521
} )
515
522
516
- You can verify the addition of the ``_id`` field during the operation
517
- by querying the ``csbios`` collection:
518
-
519
- .. code-block:: javascript
520
-
521
- db.csbios.find( { name: { first: 'Guido', last: 'van Rossum'} } )
522
-
523
- The returned document contains an ``_id`` field with the generated
524
- ``ObjectId`` value:
525
-
526
- .. code-block:: javascript
527
-
528
- {
529
- "_id" : ObjectId("507c387cbcf86cd7994f6c0b"),
530
- "name" : { "first" : "Guido", "last" : "van Rossum" },
531
- "birth" : ISODate("1956-01-31T05:00:00Z"),
532
- "contribs" : [ "Python" ],
533
- "awards" :[
534
- { "award" : "Award for the Advancement of Free Software",
535
- "year" : "2001",
536
- "by" : "Free Software Foundation" },
537
- { "award" : "NLUUG Award",
538
- "year" : "2003",
539
- "by" : "NLUUG" }
540
- ]
541
- }
542
-
543
523
- If the ``document`` contains an ``_id`` field but has a value not
544
524
found in the collection, the :method:`save() <db.collection.save()>`
545
- method performs an insert.
525
+ method performs an insert. Refer to the :ref:`insert
526
+ <crud-create-insert>` section for details of the insert operation.
546
527
547
528
The following operation performs an insert into the ``csbios``
548
529
collection since the document contains an ``_id`` field whose value
0 commit comments