Skip to content

Commit 625f7af

Browse files
committed
DOCS-875 operator
1 parent ed1b29f commit 625f7af

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
============
2+
$setOnInsert
3+
============
4+
5+
.. default-domain:: mongodb
6+
7+
.. operator:: $setOnInsert
8+
9+
.. versionadded:: 2.4
10+
11+
The :operator:`$setOnInsert` operator assigns values to fields
12+
during an :method:`upsert <db.collection.update()>` **only** when
13+
the :method:`upsert <db.collection.update()>` performs an insert.
14+
15+
.. code-block:: javascript
16+
17+
db.collection.update( <query>,
18+
{ $setOnInsert: { <field1>: <value1>, ... } },
19+
{ upsert: true }
20+
)
21+
.. example::
22+
23+
A collection named ``products`` contains no documents.
24+
25+
Then, the following :method:`upsert <db.collection.update()>`
26+
operation performs an insert and applies the
27+
:operator:`$setOnInsert` to set the field ``defaultQty`` to
28+
``100``:
29+
30+
.. code-block:: javascript
31+
32+
db.products.update(
33+
{ _id: 1 },
34+
{ $setOnInsert: { defaultQty: 100 } },
35+
{ upsert: true }
36+
)
37+
38+
The ``products`` collection contains the newly-inserted document:
39+
40+
.. code-block:: javascript
41+
42+
{ "_id" : 1, "defaultQty" : 100 }
43+
44+
.. note::
45+
46+
The :operator:`$setOnInsert` operator performs no operation for
47+
:method:`upserts <db.collection.update()>` that only perform an
48+
update and for :method:`updates <db.collection.update()>` where
49+
``upsert`` is not specified or is set to ``false``.
50+
51+
.. example::
52+
53+
A collection named ``products`` has the following document:
54+
55+
.. code-block:: javascript
56+
57+
{ "_id" : 1, "defaultQty" : 100 }
58+
59+
The following :method:`upsert <db.collection.update()>` operation
60+
performs an update:
61+
62+
.. code-block:: javascript
63+
64+
db.products.update(
65+
{ _id: 1 },
66+
{ $setOnInsert: { defaultQty: 500, inStock: true },
67+
$set: { item: "apple" } },
68+
{ upsert: true }
69+
)
70+
71+
Because the :method:`upsert <db.collection.update()>` performs an
72+
update only, the upsert ignores the :operator:`$setOnInsert`
73+
operation and only applies the :operator:`$set` operation.
74+
75+
The ``products`` collection contains the following modified
76+
document:
77+
78+
.. code-block:: javascript
79+
80+
{ "_id" : 1, "defaultQty" : 100, "item" : "apple" }

source/release-notes/2.4.txt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1431,3 +1431,42 @@ key. Consider the following properties when using a hashed shard key:
14311431
Avoid using hashed shard keys when the hashed field has non-integral floating
14321432
point values, see :ref:`hashed indexes <hashed-index-warning>` for
14331433
more information.
1434+
1435+
New Update Operators
1436+
~~~~~~~~~~~~~~~~~~~~
1437+
1438+
``$setOnInsert``
1439+
````````````````
1440+
1441+
To set fields *only* when an :method:`upsert <db.collection.update()>`
1442+
performs an insert, use the :operator:`$setOnInsert` operator with the
1443+
:method:`upsert <db.collection.update()>` .
1444+
1445+
.. example::
1446+
1447+
A collection named ``coll`` has no documents with ``_id`` equal to
1448+
``1``.
1449+
1450+
The following :method:`upsert <db.collection.update()>` operation
1451+
inserts a document and applies the :operator:`$setOnInsert` operator
1452+
to set the fields ``x`` and ``y``:
1453+
1454+
.. code-block:: javascript
1455+
1456+
db.coll.update( { _id: 1 },
1457+
{ $setOnInsert: { x: 25, y: 30 } },
1458+
{ upsert: true } )
1459+
1460+
The newly-inserted document has the field ``x`` set to ``25`` and
1461+
the field ``y`` set to 30:
1462+
1463+
.. code-block:: javascript
1464+
1465+
{ "_id" : 1, "x" : 25, "y" : 30 }
1466+
1467+
.. note::
1468+
1469+
The :operator:`$setOnInsert` operator performs no operation for
1470+
:method:`upserts <db.collection.update()>` that only perform an
1471+
update and for :method:`updates <db.collection.update()>` where
1472+
``upsert`` is not specified or is set to ``false``.

0 commit comments

Comments
 (0)