@@ -42,6 +42,40 @@ Insert a Single Document
42
42
ObjectId value to the new document. See
43
43
:ref:`write-op-insert-behavior`.
44
44
45
+ - id: compass
46
+ content: |
47
+ To insert a single document using :ref:`MongoDB Compass <compass-index>`:
48
+
49
+ 1. Navigate to the collection you wish to insert the document
50
+ into:
51
+
52
+ a. In the left-hand MongoDB Compass navigation pane, click
53
+ the database to which your target collection belongs.
54
+
55
+ b. From the database view, click the target collection name.
56
+
57
+ 2. Click the :guilabel:`Insert Document` button:
58
+
59
+ .. figure:: /images/compass-insert-button.png
60
+
61
+ 3. For each field in the document, select the field type and
62
+ fill in the field name and value. Add fields by clicking
63
+ the last line number, then clicking
64
+ :guilabel:`Add Field After ...`
65
+
66
+ - For ``Object`` types, add nested fields by clicking the
67
+ last field's number and selecting
68
+ :guilabel:`Add Field After ...`
69
+
70
+ - For ``Array`` types, add additional elements to the array
71
+ by clicking the last element's line number and selecting
72
+ :guilabel:`Add Array Element After ...`
73
+
74
+ 4. Once all fields have been filled out, click :guilabel:`Insert`.
75
+
76
+ The following example inserts a new document into the
77
+ ``test.inventory`` collection:
78
+
45
79
- id: python
46
80
content: |
47
81
:py:meth:`pymongo.collection.Collection.insert_one` inserts a
@@ -75,7 +109,7 @@ Insert a Single Document
75
109
76
110
.. code-block:: json
77
111
78
- { item: "canvas", qty: 100, tags: ["cotton"], size: { h: 28, w: 35.5, uom: "cm" } }
112
+ { item: "canvas", qty: 100, tags: ["cotton"], size: { h: 28, w: 35.5, uom: "cm" } }
79
113
80
114
The following example inserts the document above into the
81
115
``inventory`` collection. If the document does not specify
@@ -124,13 +158,13 @@ Insert a Single Document
124
158
:ruby-api:`Mongo::Collection#insert_one()<Collection.html#insert_one-instance_method>`
125
159
inserts a *single* :ref:`document<bson-document-format>` into a
126
160
collection.
127
-
161
+
128
162
The following example inserts a new document into the
129
163
``inventory`` collection. If the document does not specify
130
164
an ``_id`` field, the Ruby driver adds the ``_id`` field
131
165
with an ObjectId value to the new document. See
132
166
:ref:`write-op-insert-behavior`.
133
-
167
+
134
168
- id: scala
135
169
content: |
136
170
:scala-api:`collection.insertOne()<insertOne(document:TResult,options:org.mongodb.scala.model.InsertOneOptions):org.mongodb.scala.SingleObservable[org.mongodb.scala.Completed]>`
@@ -167,6 +201,20 @@ Insert a Single Document
167
201
an example of a return document, see
168
202
:ref:`db.collection.insertOne() reference<insertOne-examples>`.
169
203
204
+ - id: compass
205
+ content: |
206
+ .. note::
207
+
208
+ MongoDB Compass generates the ``_id`` field and its value
209
+ automatically. The generated
210
+ :term:`ObjectId` consists of a
211
+ unique randomly generated hexadecimal value.
212
+
213
+ You can change this value prior to inserting your document
214
+ so long as it remains unique and is a valid ``ObjectId``.
215
+ For more information on the ``_id`` field, see
216
+ :ref:`_id Field <insert-id-field>`.
217
+
170
218
- id: python
171
219
content: |
172
220
:py:meth:`~pymongo.collection.Collection.insert_one` returns an
@@ -179,7 +227,7 @@ Insert a Single Document
179
227
180
228
- id: java-async
181
229
content: |
182
- `com.mongodb.reactivestreams.client.MongoCollection.insertOne <http://mongodb.github.io/mongo-java-driver-reactivestreams/1.6/javadoc/com/mongodb/reactivestreams/client/MongoCollection.html#insertOne(TDocument)>`_
230
+ `com.mongodb.reactivestreams.client.MongoCollection.insertOne <http://mongodb.github.io/mongo-java-driver-reactivestreams/1.6/javadoc/com/mongodb/reactivestreams/client/MongoCollection.html#insertOne(TDocument)>`_
183
231
returns a `Publisher <http://www.reactive-streams.org/reactive-streams-1.0.1-javadoc/org/reactivestreams/Publisher.html>`_
184
232
object. The ``Publisher`` inserts the document into a collection when subscribers request data.
185
233
@@ -210,7 +258,7 @@ Insert a Single Document
210
258
211
259
- id: ruby
212
260
content: |
213
- Upon successful insert, the
261
+ Upon successful insert, the
214
262
:ruby-api:`insert_one()<Collection.html#insert_one-instance_method>`
215
263
method returns an instance of
216
264
:ruby-api:`Mongo::Operation::Result<Operation/Result.html>`, whose
@@ -219,7 +267,7 @@ Insert a Single Document
219
267
220
268
- id: scala
221
269
content: |
222
- Upon successful insert, the
270
+ Upon successful insert, the
223
271
:scala-api:`collection.insertOne()<MongoCollection.html#insertOne(document:TResult,options:org.mongodb.scala.model.InsertOneOptions):org.mongodb.scala.SingleObservable[org.mongodb.scala.Completed]>`
224
272
method returns an instance of
225
273
:scala-api:`collection.insertOne().results();<InsertOneResult>` whose
@@ -239,13 +287,13 @@ To retrieve the document that you just inserted, :ref:`query the collection
239
287
Insert Multiple Documents
240
288
-------------------------
241
289
242
- .. versionadded:: 3.2
243
-
244
290
.. tabs-drivers::
245
291
246
292
tabs:
247
293
- id: shell
248
294
content: |
295
+ .. versionadded:: 3.2
296
+
249
297
:method:`db.collection.insertMany()` can insert *multiple*
250
298
:ref:`documents <bson-document-format>` into a collection. Pass an
251
299
array of documents to the method.
@@ -255,8 +303,15 @@ Insert Multiple Documents
255
303
``_id`` field, MongoDB adds the ``_id`` field with an ObjectId
256
304
value to each document. See :ref:`write-op-insert-behavior`.
257
305
306
+ - id: compass
307
+ content: |
308
+ Currently, MongoDB Compass does not support inserting multiple
309
+ documents in a single operation.
310
+
258
311
- id: python
259
312
content: |
313
+ .. versionadded:: 3.2
314
+
260
315
:py:meth:`pymongo.collection.Collection.insert_many` can insert
261
316
*multiple* :ref:`documents <bson-document-format>` into a
262
317
collection. Pass an iterable of documents to the method.
@@ -268,6 +323,8 @@ Insert Multiple Documents
268
323
269
324
- id: java-sync
270
325
content: |
326
+ .. versionadded:: 3.2
327
+
271
328
com.mongodb.client.MongoCollection.insertMany_
272
329
can insert *multiple* :ref:`documents <bson-document-format>`
273
330
into a collection. Pass a list of documents to the method.
@@ -279,11 +336,12 @@ Insert Multiple Documents
279
336
280
337
- id: java-async
281
338
content: |
339
+ .. versionadded:: 3.2
282
340
283
341
`com.mongodb.reactivestreams.client.MongoCollection.html.insertMany
284
342
<http://mongodb.github.io/mongo-java-driver-reactivestreams/1.6/javadoc/com/mongodb/reactivestreams/client/MongoCollection.html#insertMany(java.util.List)>`_
285
343
inserts the following documents with the `Java Reactive Streams
286
- Driver <http://mongodb.github.io/mongo-java-driver-reactivestreams/1.6/>`_:
344
+ Driver <http://mongodb.github.io/mongo-java-driver-reactivestreams/1.6/>`_:
287
345
288
346
.. code-block:: json
289
347
@@ -298,6 +356,8 @@ Insert Multiple Documents
298
356
299
357
- id: nodejs
300
358
content: |
359
+ .. versionadded:: 3.2
360
+
301
361
:node-api:`Collection.insertMany() <Collection.html#insertMany>`
302
362
can insert *multiple* :ref:`documents <bson-document-format>`
303
363
into a collection. Pass an array of documents to the method.
@@ -310,6 +370,8 @@ Insert Multiple Documents
310
370
311
371
- id: php
312
372
content: |
373
+ .. versionadded:: 3.2
374
+
313
375
:phpmethod:`MongoDB\\Collection::insertMany() <phpmethod.MongoDB\\Collection::insertMany>`
314
376
can insert *multiple* :ref:`documents <bson-document-format>` into a
315
377
collection. Pass an array of documents to the method.
@@ -321,6 +383,8 @@ Insert Multiple Documents
321
383
322
384
- id: perl
323
385
content: |
386
+ .. versionadded:: 3.2
387
+
324
388
:perl-api:`MongoDB::Collection::insert_many()<Collection#insert_many>`
325
389
can insert *multiple* :ref:`documents <bson-document-format>` into a
326
390
collection. Pass an array reference of documents to the method.
@@ -332,6 +396,8 @@ Insert Multiple Documents
332
396
333
397
- id: ruby
334
398
content: |
399
+ .. versionadded:: 3.2
400
+
335
401
:ruby-api:`Mongo::Collection#insert_many()<Collection.html#insert_many-instance_method>`
336
402
can insert *multiple* :ref:`documents <bson-document-format>` into a
337
403
collection. Pass an array of documents to the method.
@@ -343,17 +409,21 @@ Insert Multiple Documents
343
409
344
410
- id: scala
345
411
content: |
412
+ .. versionadded:: 3.2
413
+
346
414
:scala-api:`collection.insertMany()<insertMany(documents:Seq[_<:TResult],options:org.mongodb.scala.model.InsertManyOptions):org.mongodb.scala.SingleObservable[org.mongodb.scala.Completed]>`
347
415
can insert *multiple* :ref:`documents <bson-document-format>` into a
348
416
collection.
349
-
417
+
350
418
The following example inserts three new documents into the
351
419
``inventory`` collection. If the documents do not specify an
352
420
``_id`` field, the Scala driver adds the ``_id`` field with
353
421
an ObjectId value to each document. See :ref:`write-op-insert-behavior`.
354
422
355
423
- id: csharp
356
424
content: |
425
+ .. versionadded:: 3.2
426
+
357
427
:csharp-api:`IMongoCollection.InsertMany()<M_MongoDB_Driver_IMongoCollection_1_InsertMany>`
358
428
can insert *multiple* :ref:`documents <bson-document-format>`
359
429
into a collection. Pass an enumerable collection of documents
@@ -375,31 +445,46 @@ Insert Multiple Documents
375
445
the newly inserted documents ``_id`` field values. See the
376
446
:ref:`reference <insertMany-examples>` for an example.
377
447
448
+ To retrieve the inserted documents, :ref:`query the collection
449
+ <read-operations-query-document>`:
450
+
378
451
- id: python
379
452
content: |
380
453
:py:meth:`~pymongo.collection.Collection.insert_many` returns
381
454
an instance of :py:class:`pymongo.results.InsertManyResult`
382
455
whose ``inserted_ids`` field is a list containing the ``_id``
383
456
of each newly inserted document.
384
457
458
+ To retrieve the inserted documents, :ref:`query the collection
459
+ <read-operations-query-document>`:
460
+
385
461
- id: java-sync
386
462
content: |
387
463
464
+ To retrieve the inserted documents, :ref:`query the collection
465
+ <read-operations-query-document>`:
466
+
388
467
- id: java-async
389
468
content: |
390
469
`com.mongodb.reactivestreams.client.MongoCollection.html.insertMany
391
470
<http://mongodb.github.io/mongo-java-driver-reactivestreams/1.6/javadoc/com/mongodb/reactivestreams/client/MongoCollection.html#insertMany(java.util.List)>`_
392
471
returns a `Publisher <http://www.reactive-streams.org/reactive-streams-1.0.1-javadoc/org/reactivestreams/Publisher.html>`_
393
- object. The ``Publisher`` inserts the document into a collection
472
+ object. The ``Publisher`` inserts the document into a collection
394
473
when subscribers request data.
395
474
475
+ To retrieve the inserted documents, :ref:`query the collection
476
+ <read-operations-query-document>`:
477
+
396
478
- id: nodejs
397
479
content: |
398
480
:node-api:`insertMany() <Collection.html#insertMany>` returns a
399
481
promise that provides a ``result``. The ``result.insertedIds``
400
482
field contains an array with the ``_id`` of each newly inserted
401
483
document.
402
484
485
+ To retrieve the inserted documents, :ref:`query the collection
486
+ <read-operations-query-document>`:
487
+
403
488
- id: php
404
489
content: |
405
490
Upon successful insert, the
@@ -411,6 +496,9 @@ Insert Multiple Documents
411
496
:phpmethod:`getInsertedIds() <phpmethod.MongoDB\\InsertManyResult::getInsertedIds>`
412
497
method returns the ``_id`` of each newly inserted document.
413
498
499
+ To retrieve the inserted documents, :ref:`query the collection
500
+ <read-operations-query-document>`:
501
+
414
502
- id: perl
415
503
content: |
416
504
Upon successful insert, the
@@ -420,6 +508,9 @@ Insert Multiple Documents
420
508
whose ``inserted_ids`` attribute is a list containing the
421
509
``_id`` of each newly inserted document.
422
510
511
+ To retrieve the inserted documents, :ref:`query the collection
512
+ <read-operations-query-document>`:
513
+
423
514
- id: ruby
424
515
content: |
425
516
Upon successful insert, the
@@ -429,20 +520,26 @@ Insert Multiple Documents
429
520
whose ``inserted_ids`` attribute is a list containing the
430
521
``_id`` of each newly inserted document.
431
522
523
+ To retrieve the inserted documents, :ref:`query the collection
524
+ <read-operations-query-document>`:
525
+
432
526
- id: scala
433
527
content: |
434
528
Upon successful insert, the
435
- :scala-api:`insertMany()<insertMany(documents:Seq[_<:TResult],options:org.mongodb.scala.model.InsertManyOptions):org.mongodb.scala.SingleObservable[org.mongodb.scala.Completed]>`
529
+ :scala-api:`insertMany()<insertMany(documents:Seq[_<:TResult],options:org.mongodb.scala.model.InsertManyOptions):org.mongodb.scala.SingleObservable[org.mongodb.scala.Completed]>`
436
530
method returns an `Observable <http://mongodb.github.io/mongo-scala-driver/2.1/reference/observables/>`_ with a type parameter indicating when
437
531
the operation has completed or with either a
438
- ``com.mongodb.DuplicateKeyException`` or
532
+ ``com.mongodb.DuplicateKeyException`` or
439
533
``com.mongodb.MongoException``.
440
534
535
+ To retrieve the inserted documents, :ref:`query the collection
536
+ <read-operations-query-document>`:
537
+
441
538
- id: csharp
442
539
content: |
443
540
444
- To retrieve the inserted documents, :ref:`query the collection
445
- <read-operations-query-document>`:
541
+ To retrieve the inserted documents, :ref:`query the collection
542
+ <read-operations-query-document>`:
446
543
447
544
.. include:: /includes/driver-example-query-7.rst
448
545
@@ -457,6 +554,8 @@ Collection Creation
457
554
If the collection does not currently exist, insert operations will
458
555
create the collection.
459
556
557
+ .. _insert-id-field:
558
+
460
559
``_id`` Field
461
560
~~~~~~~~~~~~~
462
561
0 commit comments