1
+ .. _golang-replacement-document:
2
+
1
3
=================
2
4
Replace Documents
3
5
=================
@@ -15,4 +17,110 @@ Replace Documents
15
17
.. meta::
16
18
:keywords: code example, go, write, add data, change
17
19
18
- .. TODO
20
+ Overview
21
+ --------
22
+
23
+ In this guide, you can learn how to use {+driver-short+} to perform a replace
24
+ operation on a document in a MongoDB collection. A replace operation performs
25
+ differently than an update operation. An update operation
26
+ modifies only the specified fields in a target document. A replace operation removes *all* fields
27
+ in the target document and replaces them with new ones.
28
+
29
+ Parameters
30
+ ~~~~~~~~~~
31
+
32
+ ``ReplaceOne()`` expects a **replacement document**, which is the document
33
+ that you want to take the place of an existing document. Replacement
34
+ documents use the following format:
35
+
36
+ .. code-block:: go
37
+
38
+ bson.D{{"<field>", "<value>"}, {"<field>", "<value>"}, ... }
39
+
40
+ Return Values
41
+ ~~~~~~~~~~~~~
42
+
43
+ ``ReplaceOne()`` returns an ``UpdateResult`` type that
44
+ contains information about the replace operation if the operation is
45
+ successful. The ``UpdateResult`` type contains the following properties:
46
+
47
+ .. list-table::
48
+ :widths: 30 70
49
+ :header-rows: 1
50
+
51
+ * - Property
52
+ - Description
53
+
54
+ * - ``MatchedCount``
55
+ - The number of documents matched by the filter
56
+
57
+ * - ``ModifiedCount``
58
+ - The number of documents modified by the operation
59
+
60
+ * - ``UpsertedCount``
61
+ - The number of documents upserted by the operation
62
+
63
+ * - ``UpsertedID``
64
+ - The ``_id`` of the upserted document, or ``nil`` if there is none
65
+
66
+ If multiple documents match the query filter passed to ``ReplaceOne()``,
67
+ the method selects and replaces the first matched document. Your replace
68
+ operation fails if no documents match the query filter.
69
+
70
+ Example
71
+ ~~~~~~~
72
+
73
+ The following document describes a kitchen item:
74
+
75
+ .. code-block:: json
76
+ :copyable: false
77
+
78
+ {
79
+ "_id" : 2056,
80
+ "item" : "Mug",
81
+ "brand" : "Simply Ceramics",
82
+ "price" : 2.99,
83
+ "material" : "Glass"
84
+ }
85
+
86
+ The following example uses the ``ReplaceOne()`` method to substitute
87
+ this document with one that contains an ``item`` field with a
88
+ value of "Cup" and a ``quantity`` field with a value of 107:
89
+
90
+ .. io-code-block::
91
+ :copyable: true
92
+
93
+ .. input::
94
+ :language: go
95
+
96
+ filter := bson.D{{"_id", 2056}}
97
+ replacement := bson.D{{"item", "Cup"}, {"quantity", 107}}
98
+
99
+ result, err := collection.ReplaceOne(context.TODO(), filter, replacement)
100
+ fmt.Printf("Documents matched: %v\n", result.MatchedCount)
101
+ fmt.Printf("Documents replaced: %v\n", result.ModifiedCount)
102
+
103
+ .. output::
104
+ :language: none
105
+ :visible: false
106
+
107
+ Documents matched: 1
108
+ Documents replaced: 1
109
+
110
+ The replaced document contains the contents of the replacement document
111
+ and the immutable ``_id`` field as follows:
112
+
113
+ .. code-block:: json
114
+ :copyable: false
115
+
116
+ {
117
+ "_id" : 2056,
118
+ "item" : "Cup",
119
+ "quantity" : 107
120
+ }
121
+
122
+ API Documentation
123
+ -----------------
124
+
125
+ To learn more about any of the methods or types discussed in this
126
+ guide, see the `ReplaceOne() <{+api+}/mongo#Collection.ReplaceOne>`__ API Documentation.
0 commit comments