forked from mongodb/docs-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsertOne.txt
70 lines (49 loc) · 1.69 KB
/
insertOne.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
.. _golang-insert-one:
=========================
Insert a Document Example
=========================
.. default-domain:: mongodb
You can insert a document into a collection by using the ``InsertOne()``
method.
Example
-------
.. include:: /includes/usage-examples/run-example-tip.rst
This example uses the following ``Restaurant`` struct as a model for documents
in the ``restaurants`` collection:
.. literalinclude:: /includes/usage-examples/code-snippets/insertOne.go
:start-after: start-restaurant-struct
:end-before: end-restaurant-struct
:language: go
:copyable:
:dedent:
The ``omitempty`` :ref:`struct tag<golang-struct-tags>` omits the corresponding
field from the inserted document when left empty.
The following example inserts a new document to the ``restaurants`` collection:
.. include:: /includes/fundamentals/automatic-db-coll-creation.rst
.. literalinclude:: /includes/usage-examples/code-snippets/insertOne.go
:start-after: begin insertOne
:end-before: end insertOne
:emphasize-lines: 4
:language: go
:dedent:
View a `fully runnable example <{+example+}/insertOne.go>`__
Expected Result
---------------
After you run the full example, you can find the following inserted
document in the ``restaurants`` collection:
.. code-block:: json
:copyable: false
{
"_id": ObjectId("..."),
"name": "8282",
"cuisine": "Korean"
}
For an example on how to find a document, see the :ref:`golang-find-one` usage
example.
Additional Information
----------------------
To learn more about inserting documents, see
:ref:`inserting documents <golang-insert-guide>`.
API Documentation
~~~~~~~~~~~~~~~~~
`InsertOne() <{+api+}/mongo#Collection.InsertOne>`__