Skip to content

Commit 8ad9f9d

Browse files
committed
edits
1 parent 95a2523 commit 8ad9f9d

File tree

2 files changed

+11
-124
lines changed

2 files changed

+11
-124
lines changed

snooty.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ title = "Go Driver"
33
toc_landing_pages = [
44
"/connect",
55
"/crud",
6+
"/crud/update",
67
"/monitoring-and-logging",
78
"/security",
89
"/security/authentication",

source/crud/update.txt

Lines changed: 10 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,11 @@ Update Documents
2929
Overview
3030
--------
3131

32-
In this guide, you can learn how to modify documents in MongoDB using
33-
**update** and **replace** operations.
32+
In this guide, you can learn how to update documents in MongoDB using
33+
**update** operations.
3434

3535
Update operations change the fields that you specify while leaving other
36-
fields and values unchanged. Replace operations remove all existing fields
37-
except for ``_id`` in a document and substitute the deleted fields with
38-
the new fields and values you specify.
36+
fields and values unchanged.
3937

4038
In MongoDB, all methods to modify documents follow the same pattern:
4139

@@ -52,23 +50,19 @@ The pattern expects you to:
5250
* Specify the field and value changes.
5351
* Specify options, if you must modify the method behavior.
5452

55-
The driver provides the following methods to modify documents:
53+
The driver provides the following methods to update documents:
5654

5755
* ``UpdateByID()``
5856
* ``UpdateOne()``
5957
* ``UpdateMany()``
60-
* ``ReplaceOne()``
61-
* ``BulkWrite()`` *(not discussed in this guide)*
62-
* ``FindOneAndUpdate()`` *(not discussed in this guide)*
63-
* ``FindOneAndReplace()`` *(not discussed in this guide)*
6458

6559
A Note About ``_id``
6660
~~~~~~~~~~~~~~~~~~~~
6761

6862
Each document in a MongoDB collection has a unique and immutable ``_id``
69-
field. You cannot use update and replace operations to change the
70-
``_id`` field. If you attempt to change this field, the update and
71-
replace methods return a ``WriteError``.
63+
field. You cannot use update operations to change the
64+
``_id`` field. If you attempt to change this field, the update
65+
methods return a ``WriteError``.
7266

7367
.. _golang-update-documents:
7468

@@ -151,7 +145,7 @@ See our :ref:`upsert guide <golang-upsert-guide>`
151145
to learn how to insert a new document if no documents match the query filter.
152146

153147
UpdateOne() Example
154-
-------------------
148+
~~~~~~~~~~~~~~~~~~~
155149

156150
The following document describes an employee:
157151

@@ -210,7 +204,7 @@ The following shows the updated document resulting from the preceding update ope
210204
}
211205

212206
UpdateMany() Example
213-
--------------------
207+
~~~~~~~~~~~~~~~~~~~~
214208

215209
The following example uses the ``listingsAndReviews`` collection in the
216210
``sample_airbnb`` dataset from the :atlas:`Atlas sample datasets </sample-data>`.
@@ -242,121 +236,14 @@ documents in the ``listingsAndReviews`` collection:
242236

243237
For an example on how to find multiple documents, see :ref:`golang-find-example`.
244238

245-
.. _golang-replacement-document:
246-
247-
Replace
248-
-------
249-
250-
Use the ``ReplaceOne()`` method to replace a single document.
251-
252-
Parameters
253-
~~~~~~~~~~
254-
255-
``ReplaceOne()`` expects a **replacement document**, which is the document
256-
that you want to take the place of an existing document. Replacement
257-
documents use the following format:
258-
259-
.. code-block:: go
260-
261-
bson.D{{"<field>", "<value>"}, {"<field>", "<value>"}, ... }
262-
263-
Return Values
264-
~~~~~~~~~~~~~
265-
266-
``ReplaceOne()`` returns an ``UpdateResult`` type that
267-
contains information about the replace operation if the operation is
268-
successful. The ``UpdateResult`` type contains the following properties:
269-
270-
.. list-table::
271-
:widths: 30 70
272-
:header-rows: 1
273-
274-
* - Property
275-
- Description
276-
277-
* - ``MatchedCount``
278-
- The number of documents matched by the filter
279-
280-
* - ``ModifiedCount``
281-
- The number of documents modified by the operation
282-
283-
* - ``UpsertedCount``
284-
- The number of documents upserted by the operation
285-
286-
* - ``UpsertedID``
287-
- The ``_id`` of the upserted document, or ``nil`` if there is none
288-
289-
If multiple documents match the query filter passed to ``ReplaceOne()``,
290-
the method selects and replaces the first matched document. Your replace
291-
operation fails if no documents match the query filter.
292-
293-
Example
294-
```````
295-
296-
The following document describes a kitchen item:
297-
298-
.. code-block:: json
299-
:copyable: false
300-
301-
{
302-
"_id" : 2056,
303-
"item" : "Mug",
304-
"brand" : "Simply Ceramics",
305-
"price" : 2.99,
306-
"material" : "Glass"
307-
}
308-
309-
The following example uses the ``ReplaceOne()`` method to substitute
310-
this document with one that contains an ``item`` field with a
311-
value of "Cup" and a ``quantity`` field with a value of 107:
312-
313-
.. io-code-block::
314-
:copyable: true
315-
316-
.. input::
317-
:language: go
318-
319-
filter := bson.D{{"_id", 2056}}
320-
replacement := bson.D{{"item", "Cup"}, {"quantity", 107}}
321-
322-
result, err := collection.ReplaceOne(context.TODO(), filter, replacement)
323-
fmt.Printf("Documents matched: %v\n", result.MatchedCount)
324-
fmt.Printf("Documents replaced: %v\n", result.ModifiedCount)
325-
326-
.. output::
327-
:language: none
328-
:visible: false
329-
330-
Documents matched: 1
331-
Documents replaced: 1
332-
333-
The replaced document contains the contents of the replacement document
334-
and the immutable ``_id`` field as follows:
335-
336-
.. code-block:: json
337-
:copyable: false
338-
339-
{
340-
"_id" : 2056,
341-
"item" : "Cup",
342-
"quantity" : 107
343-
}
344-
345239
Additional Information
346240
----------------------
347241

348-
For runnable examples of the update and replace operations, see the
349-
following usage examples:
350-
351-
- :ref:`golang-update-one`
352-
- :ref:`golang-update-many`
353-
- :ref:`golang-replacement-document`
354-
355242
To learn more about the operations mentioned, see the following
356243
guides:
357244

358245
- :ref:`golang-query-document`
359-
- :ref:`golang-upsert`
246+
- :ref:`golang-retrieve`
360247
- :ref:`golang-compound-operations`
361248
- :manual:`Update Operators </reference/operator/update/#update-operators>`
362249

@@ -373,4 +260,3 @@ guide, see the following API Documentation:
373260
- `UpdateByID() <{+api+}/mongo#Collection.UpdateByID>`__
374261
- `UpdateMany() <{+api+}/mongo#Collection.UpdateMany>`__
375262
- `UpdateResult <{+api+}/mongo#UpdateResult>`__
376-
- `ReplaceOne() <{+api+}/mongo#Collection.ReplaceOne>`__

0 commit comments

Comments
 (0)