4
4
Insert Documents
5
5
================
6
6
7
- .. default-domain:: mongodb
8
-
9
7
.. contents:: On this page
10
8
:local:
11
9
:backlinks: none
@@ -15,8 +13,22 @@ Insert Documents
15
13
Overview
16
14
--------
17
15
18
- In this guide, you can learn how to add documents to your MongoDB
19
- collections using insert operations.
16
+ In this guide, you can learn how to use the {+driver-long+} to add
17
+ documents to a MongoDB collection by performing insert operations.
18
+
19
+ An insert operation inserts one or more documents into a MongoDB collection.
20
+ The {+driver-short+} provides the following methods to perform insert
21
+ operations, each of which has an asynchronous and synchronous version:
22
+
23
+ - ``InsertOneAsync()`` or ``InsertOne()``
24
+ - ``InsertManyAsync()`` or ``InsertMany()``
25
+
26
+ .. tip:: Interactive Lab
27
+
28
+ You can complete a short interactive lesson that demonstrates how to
29
+ insert data by using the ``InsertOneAsync()`` method in an in-browser coding
30
+ experience. To learn more, see the :ref:`csharp-insert-instruqt-lab`
31
+ section of this guide.
20
32
21
33
Sample Data
22
34
~~~~~~~~~~~
@@ -78,18 +90,8 @@ To learn more about the ``_id`` field, see the Server Manual Entry on
78
90
To learn more about document structure and rules, see the
79
91
Server Manual Entry on :manual:`Documents </core/document>`.
80
92
81
- Insert Operations
82
- -----------------
83
-
84
- Use insert operations to add documents to a collection. You can perform
85
- inserts in MongoDB with the following methods:
86
-
87
- - ``InsertOne()``, which inserts a single document
88
- - ``InsertMany()``, which inserts multiple documents stored in an
89
- ``IEnumerable`` interface
90
-
91
93
Insert One Document
92
- ~~~~~~~~~~~~~~~~~~~
94
+ -------------------
93
95
94
96
The following code shows how to use the asynchronous
95
97
``InsertOneAsync()`` method or the synchronous ``InsertOne()`` method to
@@ -114,7 +116,7 @@ insert one document.
114
116
_restaurantsCollection.InsertOne(document);
115
117
116
118
Insert Multiple Documents
117
- ~~~~~~~~~~~~~~~~~~~~~~~~~
119
+ -------------------------
118
120
119
121
The following code shows how to use the asynchronous
120
122
``InsertManyAsync()`` method or the synchronous ``InsertMany()`` method to
@@ -143,8 +145,8 @@ insert multiple documents.
143
145
Find runnable examples using these methods under :ref:`additional
144
146
information <additional-info>`.
145
147
146
- Parameters
147
- ~~~~~~~~~~
148
+ Modify Insert Behavior
149
+ ----------------------
148
150
149
151
The ``InsertOne()`` method takes the document you seek to insert as a
150
152
parameter. The ``InsertMany()`` method takes an ``IEnumerable``
@@ -216,8 +218,8 @@ see :ref:`csharp-find-one`.
216
218
217
219
.. _csharp-ordered-behavior:
218
220
219
- Ordered Behavior
220
- ~~~~~~~~~~~~~~~~
221
+ Specify Ordered Behavior
222
+ ------------------------
221
223
222
224
Assume you want to insert the following documents:
223
225
@@ -256,6 +258,22 @@ If you look inside your collection, you should be able to see the following docu
256
258
{ "_id" : 2, "name" : "Restaurant B" }
257
259
{ "_id" : 3, "name" : "Restaurant D" }
258
260
261
+ .. _csharp-insert-instruqt-lab:
262
+
263
+ Complete an Interactive Lesson
264
+ ------------------------------
265
+
266
+ This lab helps you understand how to use the {+driver-short+} to perform insert operations in MongoDB
267
+ by using the ``InsertOneAsync()`` method. You can complete this lab directly in
268
+ your browser window without installing MongoDB or a code editor.
269
+
270
+ .. tip::
271
+
272
+ To expand the lab to a full-screen format, click the full-screen
273
+ button, :guilabel:`⛶`, in the bottom right corner of the lab pane.
274
+
275
+ .. instruqt:: /mongodb-docs/tracks/insert-a-document---c-net-driver?token=em__BF-Ccd2b1dS3YLi
276
+
259
277
.. _additional-info:
260
278
261
279
Additional Information
0 commit comments