-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathinsertMany.txt
83 lines (58 loc) · 2.24 KB
/
insertMany.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
.. _csharp-insert-many:
=========================
Insert Multiple Documents
=========================
.. facet::
:name: genre
:values: reference
.. meta::
:keywords: code example, .NET, operation
:description: Learn to insert multiple documents into a collection using C# with synchronous and asynchronous methods.
.. contents:: On this page
:local:
:backlinks: none
:depth: 2
:class: singlecol
You can insert multiple documents into a collection by using the synchronous
``InsertMany()`` method or the asynchronous ``InsertManyAsync()`` method.
Example
-------
The following example inserts multiple documents into
the ``restaurants`` collection.
Select the :guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to see the
corresponding code.
.. tabs::
.. tab:: Synchronous
:tabid: insert-many-sync
.. literalinclude:: ../includes/code-examples/insert-many/InsertMany.cs
:start-after: start-insert-many
:end-before: end-insert-many
:language: csharp
:copyable:
:dedent:
For a fully runnable example of the ``InsertMany()`` operation, see the
`InsertMany code sample <{+example+}/insert-many/InsertMany.cs>`__.
.. tab:: Asynchronous
:tabid: insert-many-async
.. literalinclude:: ../includes/code-examples/insert-many/InsertManyAsync.cs
:start-after: start-insert-many
:end-before: end-insert-many
:language: csharp
:copyable:
:dedent:
For a fully runnable example of the ``InsertManyAsync()`` operation, see the
`InsertManyAsync code sample <{+example+}/insert-many/InsertManyAsync.cs>`__.
Expected Result
~~~~~~~~~~~~~~~
After running either of the preceding full examples, the output is as follows:
.. code-block:: none
Number of restaurants found before insert: 0
Inserting documents...
Number of restaurants inserted: 5
Additional Information
----------------------
To learn more about using builders, see :ref:`csharp-builders`.
API Documentation
-----------------
- `InsertMany() <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.IMongoCollection-1.InsertMany.html>`__
- `InsertManyAsync() <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.IMongoCollection-1.InsertManyAsync.html>`__