|
| 1 | +.. _ruby-write-insert: |
| 2 | + |
| 3 | +================ |
| 4 | +Insert Documents |
| 5 | +================ |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 2 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +.. facet:: |
| 14 | + :name: genre |
| 15 | + :values: reference |
| 16 | + |
| 17 | +.. meta:: |
| 18 | + :keywords: code example, write, save, create |
| 19 | + |
| 20 | +Overview |
| 21 | +-------- |
| 22 | + |
| 23 | +In this guide, you can learn how to use the {+driver-short+} to add |
| 24 | +documents to a MongoDB collection by performing **insert operations**. |
| 25 | + |
| 26 | +An insert operation inserts one or more documents into a MongoDB collection. |
| 27 | +You can perform an insert operation by using the following methods: |
| 28 | + |
| 29 | +- ``insert_one`` to insert a single document |
| 30 | +- ``insert_many`` to insert one or more documents |
| 31 | + |
| 32 | +Sample Data |
| 33 | +~~~~~~~~~~~ |
| 34 | + |
| 35 | +The examples in this guide use the ``restaurants`` collection in the ``sample_restaurants`` |
| 36 | +database from the :atlas:`Atlas sample datasets </sample-data>`. To access this collection |
| 37 | +from your {+language+} application, create a ``Mongo::Client`` object that connects to an Atlas cluster |
| 38 | +and assign the following values to your ``database`` and ``collection`` variables: |
| 39 | + |
| 40 | +.. literalinclude:: /includes/write/insert.rb |
| 41 | + :language: ruby |
| 42 | + :dedent: |
| 43 | + :start-after: start-db-coll |
| 44 | + :end-before: end-db-coll |
| 45 | + |
| 46 | +To learn how to create a free MongoDB Atlas cluster and load the sample datasets, see the |
| 47 | +:atlas:`Get Started with Atlas </getting-started>` guide. |
| 48 | + |
| 49 | +The _id Field |
| 50 | +------------- |
| 51 | + |
| 52 | +In a MongoDB collection, each document *must* contain an ``_id`` field |
| 53 | +with a unique field value. |
| 54 | + |
| 55 | +MongoDB allows you to manage this field in two ways: |
| 56 | + |
| 57 | +- Set the ``_id`` field for each document yourself, ensuring each |
| 58 | + value is unique. |
| 59 | +- Let the driver automatically generate unique ``BSON::ObjectId`` |
| 60 | + values for each document ``_id`` field. |
| 61 | + |
| 62 | +Unless you can guarantee uniqueness, we recommend |
| 63 | +letting the driver automatically generate ``_id`` values. |
| 64 | + |
| 65 | +.. note:: |
| 66 | + |
| 67 | + Duplicate ``_id`` values violate unique index constraints, which |
| 68 | + causes the driver to return an error. |
| 69 | + |
| 70 | +To learn more about the ``_id`` field, see the |
| 71 | +:manual:`Unique Indexes </core/index-unique/>` guide in the {+mdb-server+} manual. |
| 72 | + |
| 73 | +To learn more about document structure and rules, see the |
| 74 | +:manual:`Documents </core/document>` guide in the {+mdb-server+} manual. |
| 75 | + |
| 76 | +Insert One Document |
| 77 | +------------------- |
| 78 | + |
| 79 | +To add a single document to a MongoDB collection, call the ``insert_one`` |
| 80 | +method and pass the document you want to insert. |
| 81 | + |
| 82 | +The following example inserts a document into the ``restaurants`` collection: |
| 83 | + |
| 84 | +.. literalinclude:: /includes/write/insert.rb |
| 85 | + :language: ruby |
| 86 | + :start-after: start-insert-one |
| 87 | + :end-before: end-insert-one |
| 88 | + :dedent: |
| 89 | + :copyable: |
| 90 | + |
| 91 | +Insert Multiple Documents |
| 92 | +------------------------- |
| 93 | + |
| 94 | +To add multiple documents to a MongoDB collection, call the ``insert_many`` |
| 95 | +method and pass a list of documents you want to insert. |
| 96 | + |
| 97 | +The following example inserts two documents into the ``restaurants`` collection: |
| 98 | + |
| 99 | +.. literalinclude:: /includes/write/insert.rb |
| 100 | + :language: ruby |
| 101 | + :start-after: start-insert-many |
| 102 | + :end-before: end-insert-many |
| 103 | + :dedent: |
| 104 | + :copyable: |
| 105 | + |
| 106 | +Modify Insert Behavior |
| 107 | +---------------------- |
| 108 | + |
| 109 | +You can pass a ``Hash`` object as a parameter to the ``insert_one`` |
| 110 | +method to set options to configure the insert operation. If you don't specify any options, |
| 111 | +the driver performs the insert operation with default settings. |
| 112 | + |
| 113 | +The following table describes the options you can set to |
| 114 | +configure the ``insert_one`` operation: |
| 115 | + |
| 116 | +.. list-table:: |
| 117 | + :widths: 30 70 |
| 118 | + :header-rows: 1 |
| 119 | + |
| 120 | + * - Option |
| 121 | + - Description |
| 122 | + |
| 123 | + * - ``bypass_document_validation`` |
| 124 | + - | Instructs the driver whether to ignore document-level validation. For more information, |
| 125 | + see :manual:`Schema Validation </core/schema-validation>` in the {+mdb-server+} manual. |
| 126 | + | Defaults to ``false``. |
| 127 | + |
| 128 | + * - ``comment`` |
| 129 | + - | Sets a comment to attach to the operation. For more information, see the :manual:`insert command |
| 130 | + fields </reference/command/insert/#command-fields>` guide in the {+mdb-server+} manual. |
| 131 | + |
| 132 | + * - ``session`` |
| 133 | + - | Sets the session to use for the operation. To learn more about sessions, see |
| 134 | + :manual:`Client Sessions and Causal Consistency Guarantees </core/read-isolation-consistency-recency/#std-label-sessions>` |
| 135 | + in the {+mdb-server+} manual. |
| 136 | + |
| 137 | + * - ``write_concern`` |
| 138 | + - | Sets the write concern for the operation. For more information, see the |
| 139 | + :manual:`Write Concern </core/write-concern>` guide in the {+mdb-server+} manual. |
| 140 | + |
| 141 | +You can set the preceding settings on the ``insert_many`` method |
| 142 | +by passing a ``Hash`` as a parameter to the method call. You can also use the |
| 143 | +``ordered`` option to specify the order in which the driver |
| 144 | +inserts documents into MongoDB. |
| 145 | + |
| 146 | +Example |
| 147 | +~~~~~~~ |
| 148 | + |
| 149 | +The following code uses the ``insert_many`` method to insert |
| 150 | +three new documents into a collection. Because the ``bypass_document_validation`` |
| 151 | +option is enabled, this insert operation bypasses document-level validation. |
| 152 | + |
| 153 | +.. literalinclude:: /includes/write/insert.rb |
| 154 | + :language: ruby |
| 155 | + :start-after: start-insert-options |
| 156 | + :end-before: end-insert-options |
| 157 | + :dedent: |
| 158 | + :copyable: |
| 159 | + |
| 160 | +Additional Information |
| 161 | +---------------------- |
| 162 | + |
| 163 | +To learn more about any of the methods discussed in this |
| 164 | +guide, see the following API documentation: |
| 165 | + |
| 166 | +- `insert_one <{+api-root+}/Mongo/Collection.html#insert_one-instance_method>`_ |
| 167 | +- `insert_many <{+api-root+}/Mongo/Collection.html#insert_many-instance_method>`_ |
0 commit comments