Skip to content

Commit 79ca2dd

Browse files
author
Dave
authored
DOCSP-19887 update db collection.insert pt1 v5.1 (#75) (#81)
* DOCSP-19887-update-insert-examples-pt1 * Add more pages * Update indent format
1 parent d8b4ed3 commit 79ca2dd

10 files changed

+59
-55
lines changed

source/core/2dsphere.txt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,21 +117,18 @@ documents that store location data as :ref:`GeoJSON Point
117117

118118
.. code-block:: javascript
119119

120-
db.places.insert(
120+
db.places.insertMany( [
121121
{
122122
loc : { type: "Point", coordinates: [ -73.97, 40.77 ] },
123123
name: "Central Park",
124124
category : "Parks"
125-
}
126-
)
127-
128-
db.places.insert(
125+
},
129126
{
130127
loc : { type: "Point", coordinates: [ -73.88, 40.78 ] },
131128
name: "La Guardia Airport",
132129
category : "Airport"
133130
}
134-
)
131+
] )
135132

136133
Create a ``2dsphere`` Index
137134
~~~~~~~~~~~~~~~~~~~~~~~~~~~

source/core/index-case-insensitive.txt

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,11 @@ To use the index, queries must specify the same collation.
8585

8686
.. code-block:: javascript
8787

88-
db.fruit.insert( [ { type: "apple" },
89-
{ type: "Apple" },
90-
{ type: "APPLE" } ] )
88+
db.fruit.insertMany( [
89+
{ type: "apple" },
90+
{ type: "Apple" },
91+
{ type: "APPLE" }
92+
] )
9193

9294
db.fruit.find( { type: "apple" } ) // does not use index, finds one result
9395

@@ -120,9 +122,11 @@ Insert a small collection of names:
120122

121123
.. code-block:: javascript
122124

123-
db.names.insert( [ { first_name: "Betsy" },
124-
{ first_name: "BETSY"},
125-
{ first_name: "betsy"} ] )
125+
db.names.insertMany( [
126+
{ first_name: "Betsy" },
127+
{ first_name: "BETSY"},
128+
{ first_name: "betsy"}
129+
] )
126130

127131
Queries on this collection use the specified collation by default,
128132
and if possible use the index as well.

source/core/index-partial.txt

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -267,17 +267,21 @@ fields are greater than ``21``:
267267

268268
.. code-block:: javascript
269269

270-
db.users.insert( { username: "david", age: 27 } )
271-
db.users.insert( { username: "amanda", age: 25 } )
272-
db.users.insert( { username: "rajiv", age: 32 } )
270+
db.users.insertMany( [
271+
{ username: "david", age: 27 },
272+
{ username: "amanda", age: 25 },
273+
{ username: "rajiv", age: 32 }
274+
] )
273275

274276
However, the following documents with duplicate usernames are allowed
275277
since the unique constraint only applies to documents with ``age``
276278
greater than or equal to 21.
277279

278280
.. code-block:: javascript
279281

280-
db.users.insert( { username: "david", age: 20 } )
281-
db.users.insert( { username: "amanda" } )
282-
db.users.insert( { username: "rajiv", age: null } )
282+
db.users.insertMany( [
283+
{ username: "david", age: 20 },
284+
{ username: "amanda" },
285+
{ username: "rajiv", age: null }
286+
] )
283287

source/core/index-sparse.txt

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -231,16 +231,21 @@ index permits the following :doc:`insert operations
231231

232232
.. code-block:: javascript
233233

234-
db.scores.insert( { "userid": "AAAAAAA", "score": 43 } )
235-
db.scores.insert( { "userid": "BBBBBBB", "score": 34 } )
236-
db.scores.insert( { "userid": "CCCCCCC" } )
237-
db.scores.insert( { "userid": "DDDDDDD" } )
234+
db.scores.insertMany( [
235+
{ "userid": "AAAAAAA", "score": 43 },
236+
{ "userid": "BBBBBBB", "score": 34 },
237+
{ "userid": "CCCCCCC" },
238+
{ "userid": "DDDDDDD" }
239+
] )
238240

239241
However, the index *would not permit* the addition of the following
240242
documents since documents already exists with ``score`` value of ``82``
241243
and ``90``:
242244

243245
.. code-block:: javascript
244246

245-
db.scores.insert( { "userid": "AAAAAAA", "score": 82 } )
246-
db.scores.insert( { "userid": "BBBBBBB", "score": 90 } )
247+
db.scores.insertMany( [
248+
{ "userid": "AAAAAAA", "score": 82 },
249+
{ "userid": "BBBBBBB", "score": 90 }
250+
] )
251+

source/core/index-unique.txt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,10 @@ the collection since the index enforces uniqueness for the
9393

9494
.. code-block:: javascript
9595

96-
db.collection.insert( { _id: 2, a: [ { loc: "A" }, { qty: 5 } ] } )
97-
db.collection.insert( { _id: 3, a: [ { loc: "A", qty: 10 } ] } )
96+
db.collection.insertMany( [
97+
{ _id: 2, a: [ { loc: "A" }, { qty: 5 } ] },
98+
{ _id: 3, a: [ { loc: "A", qty: 10 } ] }
99+
] )
98100

99101
.. seealso::
100102

@@ -168,7 +170,7 @@ value of ``{ "a.loc": "B", "a.qty": null }``.
168170

169171
.. code-block:: javascript
170172

171-
db.collection.insert( { _id: 4, a: [ { loc: "B" }, { loc: "B" } ] } )
173+
db.collection.insertOne( { _id: 4, a: [ { loc: "B" }, { loc: "B" } ] } )
172174

173175
.. _unique-index-and-missing-field:
174176

@@ -194,15 +196,15 @@ field ``x``:
194196

195197
.. code-block:: javascript
196198

197-
db.collection.insert( { y: 1 } )
199+
db.collection.insertOne( { y: 1 } )
198200

199201
However, the unique index errors on the insertion of a document without
200202
the field ``x`` if the collection already contains a document missing
201203
the field ``x``:
202204

203205
.. code-block:: javascript
204206

205-
db.collection.insert( { z: 1 } )
207+
db.collection.insertOne( { z: 1 } )
206208

207209
The operation fails to insert the document because of the violation of
208210
the unique constraint on the value of the field ``x``:

source/core/replica-set-write-concern.txt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,18 @@ prevent rollbacks, see :ref:`rollback-avoid`.
6868
Verify Write Operations to Replica Sets
6969
---------------------------------------
7070

71-
The following operation includes the ``writeConcern`` option to
72-
the :method:`~db.collection.insert()` method. The operation specifies
73-
:writeconcern:`"majority"` write concern and a 5 second timeout using
74-
the :ref:`wc-wtimeout` write concern parameter so that the operation
75-
does not block indefinitely.
71+
The following operation includes the ``writeConcern`` option for
72+
the :method:`~db.collection.insertOne()` method. The operation
73+
specifies:
74+
- the :writeconcern:`"majority"` write concern, and
75+
- a 5 second timeout.
76+
77+
The :ref:`wc-wtimeout` write concern parameter ensures that the
78+
operation does not block indefinitely.
7679

7780
.. code-block:: javascript
7881

79-
db.products.insert(
82+
db.products.insertOne(
8083
{ item: "envelopes", qty : 100, type: "Clasp" },
8184
{ writeConcern: { w: "majority" , wtimeout: 5000 } }
8285
)

source/core/retryable-writes.txt

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,47 +102,38 @@ cannot be :writeconcern:`{w: 0} <\<number\>>`.
102102
- Descriptions
103103

104104
* - | :method:`db.collection.insertOne()`
105-
| :method:`db.collection.insert()`
106105
| :method:`db.collection.insertMany()`
107-
108106
- Insert operations.
109107

110108
* - | :method:`db.collection.updateOne()`
111109
| :method:`db.collection.replaceOne()`
112110
| :method:`db.collection.update()` where ``multi`` is ``false``
113-
114111
- Single-document update operations. [#duplicate-key-update]_
115112

116113
* - | :method:`db.collection.deleteOne()`
117114
| :method:`db.collection.remove()` where ``justOne`` is ``true``
118-
119115
- Single document delete operations.
120116

121117
* - | :method:`db.collection.findAndModify()`
122118
| :method:`db.collection.findOneAndDelete()`
123119
| :method:`db.collection.findOneAndReplace()`
124120
| :method:`db.collection.findOneAndUpdate()`
125-
126121
- ``findAndModify`` operations. All ``findAndModify`` operations
127122
are single document operations.
128123

129124
* - :method:`db.collection.bulkWrite()` with the following write
130125
operations:
131126

132127
- :ref:`bulkwrite-write-operations-insertOne`
133-
134128
- :ref:`updateOne <bulkwrite-write-operations-updateOneMany>`
135-
136129
- :ref:`bulkwrite-write-operations-replaceOne`
137-
138130
- :ref:`deleteOne <bulkwrite-write-operations-deleteOneMany>`
139131

140132
- Bulk write operations that only consist of the single-document
141133
write operations. A retryable bulk operation can include any
142134
combination of the specified write operations but cannot include
143135
any multi-document write operations, such as ``updateMany``.
144136

145-
146137
* - :method:`Bulk <Bulk()>` operations for:
147138

148139
- :method:`Bulk.find.removeOne()`

source/core/schema-validation.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ For example, the following insert operation violates the validation rule:
342342

343343
.. code-block:: javascript
344344

345-
db.contacts2.insert( { name: "Amanda", status: "Updated" } )
345+
db.contacts2.insertOne( { name: "Amanda", status: "Updated" } )
346346

347347
However, since the ``validationAction`` is ``warn`` only, MongoDB only
348348
logs the validation violation message and allows the operation to

source/core/transactions-in-applications.txt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,8 +476,14 @@ transactions:
476476
:copyable: false
477477

478478
// Create collections:
479-
db.getSiblingDB("mydb1").foo.insert( {abc: 0}, { writeConcern: { w: "majority", wtimeout: 2000 } } );
480-
db.getSiblingDB("mydb2").bar.insert( {xyz: 0}, { writeConcern: { w: "majority", wtimeout: 2000 } } );
479+
db.getSiblingDB("mydb1").foo.insertOne(
480+
{abc: 0},
481+
{ writeConcern: { w: "majority", wtimeout: 2000 } }
482+
)
483+
db.getSiblingDB("mydb2").bar.insertOne(
484+
{xyz: 0},
485+
{ writeConcern: { w: "majority", wtimeout: 2000 } }
486+
)
481487

482488
// Start a session.
483489
session = db.getMongo().startSession( { readPreference: { mode: "primary" } } );

source/core/transactions-operations.txt

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,27 +124,19 @@ operations against a :red:`non-existing` collection:
124124
* - | :method:`db.collection.findAndModify()` with ``upsert: true``
125125
| :method:`db.collection.findOneAndReplace()` with ``upsert: true``
126126
| :method:`db.collection.findOneAndUpdate()` with ``upsert: true``
127-
128127
- :dbcommand:`findAndModify` with ``upsert: true``
129128

130-
131129
* - | :method:`db.collection.insertMany()`
132130
| :method:`db.collection.insertOne()`
133-
| :method:`db.collection.insert()`
134-
135131
- :dbcommand:`insert`
136132

137133
* - | :method:`db.collection.updateOne()` with ``upsert: true``
138134
| :method:`db.collection.updateMany()` with ``upsert: true``
139135
| :method:`db.collection.replaceOne()` with ``upsert: true``
140-
| :method:`db.collection.update()` with ``upsert: true``
141-
142136
- :dbcommand:`update` with ``upsert: true``
143137

144-
145138
* - | :method:`db.collection.bulkWrite()` with insert or ``upsert:true`` operations
146139
| Various :doc:`/reference/method/js-bulk` with insert or ``upsert:true`` operations
147-
148140
-
149141

150142
For other CRUD operations allowed in transactions, see

0 commit comments

Comments
 (0)