Skip to content

Commit 3827fc5

Browse files
authored
Add options to insert page (#61)
* InsertOneOptions * restructure page * autobuilder * remove passive voice * change toc depth * table formatting * more formatting experiments * formatting * include code snippet * nits * restructure page * formatting * remove space
1 parent 8eac273 commit 3827fc5

File tree

2 files changed

+137
-44
lines changed

2 files changed

+137
-44
lines changed

source/fundamentals/crud/write-operations/insert.txt

Lines changed: 116 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Insert a Document
77
.. contents:: On this page
88
:local:
99
:backlinks: none
10-
:depth: 1
10+
:depth: 2
1111
:class: singlecol
1212

1313
.. _insert_guide_golang:
@@ -19,9 +19,9 @@ In this guide, you can learn how to **insert** documents into a MongoDB
1919
collection.
2020

2121
Before you can find, update, and delete documents in MongoDB, you need
22-
to insert those documents. Insert operations insert one document using
23-
the ``InsertOne()`` function, or insert multiple documents using one of
24-
the ``InsertMany()`` or ``BulkWrite()`` function.
22+
to insert those documents. You can insert one document using
23+
the ``InsertOne()`` function, or insert multiple documents using either
24+
the ``InsertMany()`` or ``BulkWrite()`` functions.
2525

2626
The following sections focus on ``InsertOne()`` and ``InsertMany()``.
2727

@@ -41,7 +41,7 @@ The two options for managing this field are:
4141
driver generates unique ``ObjectId`` values for documents that you do
4242
not explicitly specify an ``_id``.
4343

44-
Unless you provide strong guarantees for uniqueness, we recommend
44+
Unless you provide strong guarantees for uniqueness, MongoDB recommends
4545
you let the driver automatically generate ``_id`` values.
4646

4747
.. note::
@@ -85,7 +85,36 @@ The output should look like this:
8585
.. code-block:: none
8686
:copyable: false
8787

88-
Inserted document with _id: ObjectID("6112c7cacf7fb532a9c0077f")
88+
Inserted document with _id: ObjectID("...")
89+
90+
Modify ``InsertOne`` Behavior
91+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
92+
You can modify the behavior of ``InsertOne()`` by constructing and passing
93+
an optional ``InsertOneOptions`` struct. The available options to tune with
94+
``InsertOneOptions`` are:
95+
96+
.. list-table::
97+
:header-rows: 1
98+
:stub-columns: 1
99+
:class: compatibility-large
100+
101+
* - Option
102+
- Description
103+
104+
* - ``BypassDocumentValidation``
105+
- | If ``true``, allows the write to opt-out of :manual:`document level validation </core/schema-validation>`.
106+
107+
| Default: ``false``
108+
109+
Construct an ``InsertOneOptions`` as follows:
110+
111+
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/insertOptions.go
112+
:start-after: // begin insertOneOpts
113+
:end-before: // end insertOneOpts
114+
:language: go
115+
:copyable:
116+
:dedent:
117+
89118

90119
Insert Multiple Documents
91120
-------------------------
@@ -95,10 +124,84 @@ collection.
95124

96125
Upon successful insertion, the ``InsertMany()`` function returns an ``InsertManyResult``
97126
instance that contains the ``_id`` fields of the inserted documents.
98-
This function inserts documents in the order specified unless an exception
99-
occurs.
100127

101-
For example, assume you want to insert the following documents:
128+
Example
129+
~~~~~~~
130+
131+
The following example creates and inserts some documents into the
132+
``favorite_books`` collection using the ``InsertMany()`` function:
133+
134+
.. code-block:: go
135+
136+
coll := client.Database("myDB").Collection("favorite_books")
137+
docs := []interface{}{
138+
bson.D{{"title", "My Brilliant Friend"}, {"author", "Elena Ferrante"}, {"year_published", 2012}},
139+
bson.D{{"title", "Lucy"}, {"author", "Jamaica Kincaid"}, {"year_published", 2002}},
140+
bson.D{{"title", "Cat's Cradle"}, {"author", "Kurt Vonnegut Jr."}, {"year_published", 1998}},
141+
}
142+
143+
result, err := coll.InsertMany(context.TODO(), docs)
144+
list_ids := result.InsertedIDs
145+
fmt.Printf("Documents inserted: %v\n", len(list_ids))
146+
147+
for _, id := range list_ids {
148+
fmt.Printf("Inserted document with _id: %v\n", id)
149+
}
150+
151+
Your output should look like this:
152+
153+
.. code-block:: none
154+
:copyable: false
155+
156+
Documents inserted: 3
157+
Inserted document with _id: ObjectID("...")
158+
Inserted document with _id: ObjectID("...")
159+
Inserted document with _id: ObjectID("...")
160+
161+
Modify ``InsertMany`` Behavior
162+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
163+
164+
You can modify the behavior of ``InsertMany()`` by constructing
165+
and passing an optional ``InsertOneOptions`` struct. The available options
166+
to tune with ``InsertOneOptions`` are:
167+
168+
.. list-table::
169+
:header-rows: 1
170+
:stub-columns: 1
171+
:class: compatibility-large
172+
173+
* - Option
174+
- Description
175+
176+
* - ``BypassDocumentValidation``
177+
- | If ``true``, allows the write to opt-out of :manual:`document level validation </core/schema-validation>`.
178+
179+
| Default: ``false``
180+
181+
* - ``Ordered``
182+
- | If ``true``, the driver sends documents to the server in the order provided.
183+
If an error occurs, the driver and server abort all remaining insert operations.
184+
See the section on :ref:`ordered behavior <ordered_behavior_go>` for more information.
185+
186+
| Default: ``false``
187+
188+
Construct an ``InsertManyOptions`` as follows:
189+
190+
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/insertOptions.go
191+
:start-after: // begin insertManyOpts
192+
:end-before: // end insertManyOpts
193+
:language: go
194+
:copyable:
195+
:dedent:
196+
197+
198+
199+
.. _ordered_behavior_go:
200+
201+
``Ordered`` Behavior
202+
~~~~~~~~~~~~~~~~~~~~
203+
204+
Assume you want to insert the following documents:
102205

103206
.. code-block:: json
104207
:copyable: false
@@ -108,9 +211,10 @@ For example, assume you want to insert the following documents:
108211
{ "_id": 1, "country": "Vietnam" }
109212
{ "_id": 3, "country": "Argentina" }
110213

111-
If you attempt to insert these documents, a ``BulkWriteException`` occurs at the
112-
third document because of the repeated ``_id`` value, but the documents
113-
before the error-producing document still get inserted into your collection.
214+
If you attempt to insert these documents with default ``InsertManyOptions``, a
215+
``BulkWriteException`` occurs at the third document because of the repeated
216+
``_id`` value, but the documents before the error-producing document still get
217+
inserted into your collection.
114218

115219
.. note::
116220

@@ -153,38 +257,6 @@ before the error-producing document still get inserted into your collection.
153257
{ "_id": 1, "country": "Tanzania" }
154258
{ "_id": 2, "country": "Lithuania" }
155259

156-
Example
157-
~~~~~~~
158-
159-
The following example creates and inserts some documents into the
160-
``favorite_books`` collection using the ``InsertMany()`` function:
161-
162-
.. code-block:: go
163-
164-
coll := client.Database("myDB").Collection("favorite_books")
165-
docs := []interface{}{
166-
bson.D{{"title", "My Brilliant Friend"}, {"author", "Elena Ferrante"}, {"year_published", 2012}},
167-
bson.D{{"title", "Lucy"}, {"author", "Jamaica Kincaid"}, {"year_published", 2002}},
168-
bson.D{{"title", "Cat's Cradle"}, {"author", "Kurt Vonnegut Jr."}, {"year_published", 1998}},
169-
}
170-
171-
result, err := coll.InsertMany(context.TODO(), docs)
172-
list_ids := result.InsertedIDs
173-
fmt.Printf("Documents inserted: %v\n", len(list_ids))
174-
175-
for _, id := range list_ids {
176-
fmt.Printf("Inserted document with _id: %v\n", id)
177-
}
178-
179-
Your output should look like this:
180-
181-
.. code-block:: none
182-
:copyable: false
183-
184-
Documents inserted: 3
185-
Inserted document with _id: ObjectID("6112d6a0acf85446f904eb91")
186-
Inserted document with _id: ObjectID("6112d6a0acf85446f904eb92")
187-
Inserted document with _id: ObjectID("6112d6a0acf85446f904eb93")
188260

189261
Additional Information
190262
----------------------
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"go.mongodb.org/mongo-driver/mongo/options"
7+
)
8+
9+
func insertManyOpts() {
10+
// begin insertManyOpts
11+
opts := options.InsertMany().SetBypassDocumentValidation(true).SetOrdered(false)
12+
// end insertManyOpts
13+
fmt.Println(opts)
14+
}
15+
16+
func insertOneOpts() {
17+
// begin insertOneOpts
18+
opts := options.InsertOne().SetBypassDocumentValidation(true)
19+
// end insertOneOpts
20+
fmt.Println(opts)
21+
}

0 commit comments

Comments
 (0)