@@ -13,25 +13,28 @@ documents from a collection in your MongoDB database.
13
13
Insert
14
14
------
15
15
16
- If you want to add new documents to a collection, you can use
17
- the ``insertOne()`` or the ``insertMany()`` method . These methods accept
16
+ To add new documents to a collection, you can use
17
+ the ``insertOne()`` or the ``insertMany()`` methods . These methods accept
18
18
single or multiple documents, respectively. The driver automatically
19
19
generates a unique ``_id`` field for documents unless specified.
20
20
21
- You can specify the document to be inserted by the ``insertOne()`` write
22
- operation in a JSON object as follows:
21
+ The following examples use a collection called ``pizzaCollection``,
22
+ which contains documents with the name and shape of a pizza.
23
+
24
+ Insert a Document Example
25
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
26
+
27
+ You can specify a document in a JSON object as follows:
23
28
24
29
.. code-block:: javascript
25
30
26
31
const pizzaDocument = {
27
32
name: "Neapolitan pizza",
28
- shape: "round",
29
- toppings: [ "San Marzano tomatoes", "mozzarella di bufala cheese" ],
33
+ shape: "round"
30
34
};
31
35
32
- To insert the document into the collection reference ``pizzaCollection``,
33
- specify the document as the first parameter of your call to the
34
- ``insertOne()`` method as shown below:
36
+ To insert this document into ``pizzaCollection``, pass ``pizzaDocument``
37
+ as the first parameter to the ``insertOne()`` method as shown below:
35
38
36
39
.. code-block:: javascript
37
40
@@ -47,20 +50,22 @@ You can print the number of documents inserted by accessing the
47
50
For a runnable example, see the :doc:`insertOne() </usage-examples/insertOne>`
48
51
usage example.
49
52
50
- You can specify the documents to be inserted by the ``insertMany()`` write
51
- operation in an array of JSON objects as follows:
53
+ Insert Multiple Documents Example
54
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
55
+
56
+ You can specify multiple documents in an array of JSON objects as follows:
52
57
53
58
.. code-block:: javascript
54
59
55
60
const pizzaDocuments = [
56
61
{ name: "Sicilian pizza", shape: "square" },
57
62
{ name: "New York pizza", shape: "round" },
58
- { name: "Grandma pizza", shape: "square" },
63
+ { name: "Grandma pizza", shape: "square" }
59
64
];
60
65
61
- To insert the array of documents into the collection reference
62
- ``pizzaCollection``, specify the array as the first parameter of your call
63
- to the ``insertMany()`` method as shown below:
66
+ To insert these documents into ``pizzaCollection``, pass
67
+ ``pizzaDocuments`` as the first parameter to the ``insertMany()`` method
68
+ as shown below:
64
69
65
70
.. code-block:: javascript
66
71
0 commit comments