|
| 1 | +.. _rust-insert-many-usage: |
| 2 | + |
| 3 | +========================= |
| 4 | +Insert Multiple Documents |
| 5 | +========================= |
| 6 | + |
| 7 | +You can insert multiple documents into a collection by calling the |
| 8 | +`insert_many() <{+api+}/struct.Collection.html#method.insert_many>`__ method |
| 9 | +on a ``Collection`` instance. |
| 10 | + |
| 11 | +Pass a vector containing one or more documents to the ``insert_many()`` method |
| 12 | +to insert them into your collection. These documents must be instances of the type |
| 13 | +that you parameterized your ``Collection`` instance with. For example, if you |
| 14 | +parameterized your collection with the ``MyStruct`` struct, pass a vector of ``MyStruct`` |
| 15 | +instances as a parameter to the ``insert_many()`` method. |
| 16 | + |
| 17 | +.. tip:: |
| 18 | + |
| 19 | + To insert a single document, consider using the `insert_one() |
| 20 | + <{+api+}/struct.Collection.html#method.insert_one>`__ method instead. For a |
| 21 | + runnable code example that uses this method, see the :ref:`rust-insert-one-usage` |
| 22 | + usage example. |
| 23 | + |
| 24 | +The ``insert_many()`` method returns an `InsertManyResult <{+api+}/results/struct.InsertManyResult.html>`__ |
| 25 | +type that references the ``_id`` values of the inserted documents. |
| 26 | + |
| 27 | +To learn more about inserting documents into a collection, see the |
| 28 | +:ref:`rust-insert-guide` guide. |
| 29 | + |
| 30 | +Example |
| 31 | +------- |
| 32 | + |
| 33 | +This example inserts documents into the ``restaurants`` collection of the |
| 34 | +``sample_restaurants`` database. The example uses a ``Restaurant`` struct containing |
| 35 | +``name`` and ``cuisine`` fields to model the documents being inserted into the |
| 36 | +collection. |
| 37 | + |
| 38 | +This example passes a vector of documents as a parameter to the ``insert_many()`` |
| 39 | +method. |
| 40 | + |
| 41 | +Select the **Asynchronous** or **Synchronous** tab to see corresponding code for each runtime: |
| 42 | + |
| 43 | +.. tabs:: |
| 44 | + |
| 45 | + .. tab:: Asynchronous |
| 46 | + :tabid: find-async |
| 47 | + |
| 48 | + .. io-code-block:: |
| 49 | + :copyable: true |
| 50 | + |
| 51 | + .. input:: /includes/usage-examples/code-snippets/insert-many-async.rs |
| 52 | + :language: rust |
| 53 | + :dedent: |
| 54 | + |
| 55 | + .. output:: |
| 56 | + :language: console |
| 57 | + :visible: false |
| 58 | + |
| 59 | + Inserted documents with _ids: |
| 60 | + ObjectId("...") |
| 61 | + ObjectId("...") |
| 62 | + |
| 63 | + .. tab:: Synchronous |
| 64 | + :tabid: find-sync |
| 65 | + |
| 66 | + .. io-code-block:: |
| 67 | + :copyable: true |
| 68 | + |
| 69 | + .. input:: /includes/usage-examples/code-snippets/insert-many-sync.rs |
| 70 | + :language: rust |
| 71 | + :dedent: |
| 72 | + |
| 73 | + .. output:: |
| 74 | + :language: console |
| 75 | + :visible: false |
| 76 | + |
| 77 | + Inserted documents with _ids: |
| 78 | + ObjectId("...") |
| 79 | + ObjectId("...") |
0 commit comments