Skip to content

DOCSP-49977 Replace One #506

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions source/connect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ Connection Guide
:titlesonly:
:maxdepth: 1

Create a MongoClient <connect/mongoclient>
Choose a Connection Target <connect/connection-targets>
Create a MongoClient </connect/mongoclient>
Choose a Connection Target </connect/connection-targets>
Connection Options </connect/specify-connection-options>
Connection Troubleshooting </connect/connection-troubleshooting>

Expand Down
9 changes: 7 additions & 2 deletions source/crud/update.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ Modify Documents
:depth: 2
:class: singlecol

.. TODO edit
.. toctree::
:caption: Update Documents

Replace Documents </crud/update/replace>
Update Arrays </crud/update/embedded-arrays>
Upsert </crud/update/upsert>

Overview
--------
Expand Down Expand Up @@ -312,7 +317,7 @@ following usage examples:

- :ref:`golang-update-one`
- :ref:`golang-update-many`
- :ref:`golang-replace`
- :ref:`golang-replacement-document`

To learn more about the operations mentioned, see the following
guides:
Expand Down
114 changes: 113 additions & 1 deletion source/crud/update/replace.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
.. _golang-replacement-document:
.. _golang-replace:

=================
Replace Documents
=================
Expand All @@ -15,4 +18,113 @@ Replace Documents
.. meta::
:keywords: code example, go, write, add data, change

.. TODO
Overview
--------

In this guide, you can learn how to use the {+driver-short+} to perform a replace
operation on a document in a MongoDB collection. A replace operation performs
differently than an update operation. An update operation
modifies only the specified fields in a target document. A replace operation removes
all fields in the target document and replaces them with new ones.

Parameters
----------

``ReplaceOne()`` expects a **replacement document**, which is the document
that you want to take the place of an existing document. Replacement
documents use the following format:

.. code-block:: go

bson.D{{"<field>", "<value>"}, {"<field>", "<value>"}, ... }

Return Values
-------------

``ReplaceOne()`` returns an ``UpdateResult`` type that
contains information about the replace operation if the operation is
successful. The ``UpdateResult`` type contains the following properties:

.. list-table::
:widths: 30 70
:header-rows: 1

* - Property
- Description

* - ``MatchedCount``
- The number of documents matched by the filter

* - ``ModifiedCount``
- The number of documents modified by the operation

* - ``UpsertedCount``
- The number of documents upserted by the operation

* - ``UpsertedID``
- The ``_id`` of the upserted document, or ``nil`` if there is none

If multiple documents match the query filter passed to ``ReplaceOne()``,
the method selects and replaces the first matched document. Your replace
operation fails if no documents match the query filter.

Example
-------

The following document describes a kitchen item:

.. code-block:: json
:copyable: false

{
"_id" : 2056,
"item" : "Mug",
"brand" : "Simply Ceramics",
"price" : 2.99,
"material" : "Glass"
}

The following example uses the ``ReplaceOne()`` method to substitute
this document with one that contains an ``item`` field with a
value of "Cup" and a ``quantity`` field with a value of 107:

.. io-code-block::
:copyable: true

.. input::
:language: go

filter := bson.D{{"_id", 2056}}
replacement := bson.D{{"item", "Cup"}, {"quantity", 107}}

result, err := collection.ReplaceOne(context.TODO(), filter, replacement)
fmt.Printf("Documents matched: %v\n", result.MatchedCount)
fmt.Printf("Documents replaced: %v\n", result.ModifiedCount)

.. output::
:language: none
:visible: false

Documents matched: 1
Documents replaced: 1

The replaced document contains the contents of the replacement document
and the immutable ``_id`` field as follows:

.. code-block:: json
:copyable: false

{
"_id" : 2056,
"item" : "Cup",
"quantity" : 107
}

API Documentation
-----------------

To learn more about any of the methods or types discussed in this
guide, see the following API Documentation:

- `ReplaceOne() <{+api+}/mongo#Collection.ReplaceOne>`__
- `UpdateResult <{+api+}/mongo#UpdateResult>`__
Loading