/
update-documents.txt
1430 lines (972 loc) · 48.8 KB
/
update-documents.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
.. include:: /includes/java-sync-links.rst
.. include:: /includes/java-async-links.rst
.. _write-op-update:
================
Update Documents
================
.. default-domain:: mongodb
.. facet::
:name: programming_language
:values: shell, csharp, go, java, python, perl, php, ruby, rust, scala, javascript/typescript, kotlin
.. meta::
:description: How to update single or multiple documents in MongoDB. How to update all or replace documents in MongoDB. How to update fields in documents in MongoDB.
:keywords: update collection, motor, java sync, java async, reactive streams, code example, node.js, compass, coroutine
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
You can update documents in MongoDB using the following methods:
- Your programming language's driver.
- The :atlas:`{+atlas+} UI </>`. To learn more, see
:ref:`update-documents-atlas-ui`.
- :compass:`MongoDB Compass </>`.
----------
.. |arrow| unicode:: U+27A4
|arrow| Use the **Select your language** drop-down menu in the
upper-right to set the language of the following examples.
----------
.. |populate-inventory| replace:: The examples on this page use the
``inventory`` collection. Connect to a test database in your MongoDB
instance then create the ``inventory`` collection:
.. tabs-selector:: drivers
.. tabs-drivers::
.. tab::
:tabid: shell
This page uses the following :binary:`~bin.mongosh` methods:
- :method:`db.collection.updateOne(\<filter\>, \<update\>, \<options\>) <db.collection.updateOne>`
- :method:`db.collection.updateMany(\<filter\>, \<update\>, \<options\>) <db.collection.updateMany>`
- :method:`db.collection.replaceOne(\<filter\>, \<update\>, \<options\>) <db.collection.replaceOne>`
|populate-inventory|
.. tab::
:tabid: compass
This page uses :ref:`MongoDB Compass <compass-index>` to
update the documents.
The examples on this page use the ``inventory`` collection.
Populate the ``inventory`` collection with the following
documents:
.. tab::
:tabid: c
This page uses the following `MongoDB C Driver <https://mongoc.org/libmongoc/current/index.html>`__
methods:
- `mongoc_collection_update_one <https://mongoc.org/libmongoc/current/mongoc_collection_update_one.html>`__
- `mongoc_collection_replace_one <https://mongoc.org/libmongoc/current/mongoc_collection_replace_one.html>`__
|populate-inventory|
.. tab::
:tabid: python
This page uses the following
`PyMongo <https://pymongo.readthedocs.io/en/stable/index.html>`__
Python driver methods:
- :py:meth:`pymongo.collection.Collection.update_one`
- :py:meth:`pymongo.collection.Collection.update_many`
- :py:meth:`pymongo.collection.Collection.replace_one`
|populate-inventory|
.. tab::
:tabid: motor
This page uses the
following `Motor <https://motor.readthedocs.io/en/stable/>`_
driver methods:
- :py:meth:`motor.motor_asyncio.AsyncIOMotorCollection.update_one`
- :py:meth:`motor.motor_asyncio.AsyncIOMotorCollection.update_many`
- :py:meth:`motor.motor_asyncio.AsyncIOMotorCollection.replace_one`
|populate-inventory|
.. tab::
:tabid: java-sync
This page uses the
following `Java Synchronous Driver`_ methods:
- com.mongodb.client.MongoCollection.updateOne_
- com.mongodb.client.MongoCollection.updateMany_
- com.mongodb.client.MongoCollection.replaceOne_
|populate-inventory|
.. tab::
:tabid: java-async
This page uses the
following `Java Reactive Streams Driver
<http://mongodb.github.io/mongo-java-driver-reactivestreams/1.6/>`_ methods:
- `com.mongodb.reactivestreams.client.MongoCollection.updateOne
<http://mongodb.github.io/mongo-java-driver-reactivestreams/1.6/javadoc/com/mongodb/reactivestreams/client/MongoCollection.html#updateOne(org.bson.conversions.Bson,%20org.bson.conversions.Bson)>`_
- `com.mongodb.reactivestreams.client.MongoCollection.updateMany
<http://mongodb.github.io/mongo-java-driver-reactivestreams/1.6/javadoc/com/mongodb/reactivestreams/client/MongoCollection.html#updateMany(org.bson.conversions.Bson,%20org.bson.conversions.Bson)>`_
- `com.mongodb.reactivestreams.client.MongoCollection.replaceOne
<http://mongodb.github.io/mongo-java-driver-reactivestreams/1.6/javadoc/com/mongodb/reactivestreams/client/MongoCollection.html#replaceOne(org.bson.conversions.Bson,%20TDocument)>`_
|populate-inventory|
.. tab::
:tabid: kotlin-coroutine
This page uses the
following :driver:`Kotlin Coroutine Driver </kotlin/coroutine/current/>` methods:
- `MongoCollection.updateOne() <{+java-api-docs+}/mongodb-driver-kotlin-coroutine/mongodb-driver-kotlin-coroutine/com.mongodb.kotlin.client.coroutine/-mongo-collection/update-one.html>`__
- `MongoCollection.updateMany() <{+java-api-docs+}/mongodb-driver-kotlin-coroutine/mongodb-driver-kotlin-coroutine/com.mongodb.kotlin.client.coroutine/-mongo-collection/update-many.html>`__
- `MongoCollection.replaceOne() <{+java-api-docs+}/mongodb-driver-kotlin-coroutine/mongodb-driver-kotlin-coroutine/com.mongodb.kotlin.client.coroutine/-mongo-collection/replace-one.html>`__
|populate-inventory|
.. tab::
:tabid: nodejs
This page uses the
following :node-docs:`MongoDB Node.js Driver <>` methods:
- :node-api:`Collection.updateOne() <Collection.html#updateOne>`
- :node-api:`Collection.updateMany() <Collection.html#updateMany>`
- :node-api:`Collection.replaceOne() <Collection.html#replaceOne>`
|populate-inventory|
.. tab::
:tabid: php
This page uses the following `MongoDB PHP Library
<https://www.mongodb.com/docs/php-library/current/>`_ methods:
- :phpmethod:`MongoDB\\Collection::updateOne() <phpmethod.MongoDB\\Collection::updateOne()>`
- :phpmethod:`MongoDB\\Collection::updateMany() <phpmethod.MongoDB\\Collection::updateMany()>`
- :phpmethod:`MongoDB\\Collection::replaceOne() <phpmethod.MongoDB\\Collection::replaceOne()>`
|populate-inventory|
.. tab::
:tabid: perl
This page uses the following `MongoDB Perl Driver
<https://metacpan.org/release/MongoDB>`_ methods:
- :perl-api:`MongoDB::Collection::update_one()<Collection#update_one>`
- :perl-api:`MongoDB::Collection::update_many()<Collection#update_many>`
- :perl-api:`MongoDB::Collection::replace_one()<Collection#replace_one>`
|populate-inventory|
.. tab::
:tabid: ruby
This page uses the following :ruby:`MongoDB Ruby Driver </>` methods:
- :ruby-api:`Mongo::Collection#update_one()<Collection.html#update_one-instance_method>`
- :ruby-api:`Mongo::Collection#update_many()<Collection.html#update_many-instance_method>`
- :ruby-api:`Mongo::Collection#replace_one()<Collection.html#replace_one-instance_method>`
|populate-inventory|
.. tab::
:tabid: scala
This page uses the following `MongoDB Scala Driver
<http://mongodb.github.io/mongo-scala-driver/>`_ methods:
- :scala-api:`collection.updateOne()<updateOne(filter:org.mongodb.scala.bson.conversions.Bson,update:org.mongodb.scala.bson.conversions.Bson,options:org.mongodb.scala.model.UpdateOptions):org.mongodb.scala.SingleObservable[org.mongodb.scala.result.UpdateResult]>`
- :scala-api:`collection.updateMany()<updateMany(filter:org.mongodb.scala.bson.conversions.Bson,update:org.mongodb.scala.bson.conversions.Bson,options:org.mongodb.scala.model.UpdateOptions):org.mongodb.scala.SingleObservable[org.mongodb.scala.result.UpdateResult]>`
- :scala-api:`collection.replaceOne()<replaceOne(filter:org.mongodb.scala.bson.conversions.Bson,replacement:TResult,options:org.mongodb.scala.model.UpdateOptions):org.mongodb.scala.SingleObservable[org.mongodb.scala.result.UpdateResult]>`
|populate-inventory|
.. tab::
:tabid: csharp
This page uses the following :driver:`MongoDB C# Driver </csharp/>` methods:
- :csharp-api:`IMongoCollection.UpdateOne()<M_MongoDB_Driver_IMongoCollection_1_UpdateOne>`
- :csharp-api:`IMongoCollection.UpdateMany()<M_MongoDB_Driver_IMongoCollection_1_UpdateMany>`
- :csharp-api:`IMongoCollection.ReplaceOne() <M_MongoDB_Driver_IMongoCollection_1_ReplaceOne>`
|populate-inventory|
.. tab::
:tabid: go
This page uses the following :driver:`MongoDB Go Driver </go/>` functions:
- :go-api:`Collection.UpdateOne<mongo#Collection.UpdateOne>`
- :go-api:`Collection.UpdateMany<mongo#Collection.UpdateMany>`
- :go-api:`Collection.ReplaceOne <mongo#Collection.ReplaceOne>`
|populate-inventory|
.. include:: /includes/driver-examples/driver-example-update-51.rst
.. _update-documents-modifiers:
Update Documents in a Collection
--------------------------------
.. tabs-drivers::
.. tab::
:tabid: shell
To update a document, MongoDB provides
:manual:`update operators </reference/operator/update>`, such
as :update:`$set`, to modify field values.
To use the update operators, pass to the update methods an
update document of the form:
.. code-block:: javascript
{
<update operator>: { <field1>: <value1>, ... },
<update operator>: { <field2>: <value2>, ... },
...
}
.. include:: /includes/fact-update-set-create-fields.rst
.. tab::
:tabid: compass
To update a document in Compass, hover over the target
document and click the pencil icon:
.. figure:: /images/compass-update-doc-button.png
:alt: Click edit document
After clicking the pencil icon, the document enters edit mode:
.. figure:: /images/compass-update-edit-mode.png
:alt: Document edit mode
You can now change the this document by clicking the item you wish
to change and modifying the value.
For detailed instructions on updating documents in Compass,
refer to the
:ref:`Compass documentation <compass-modify-documents>`
or follow the :ref:`example <write-op-updateOne>` below.
Once you are satisfied with your changes, click
:guilabel:`Update` to save the updated document.
Click :guilabel:`Cancel` to revert any modifications made to
the document and exit edit mode.
.. tab::
:tabid: python
To update a document, MongoDB provides
:manual:`update operators </reference/operator/update>` such
as :update:`$set` to modify field values.
To use the update operators, pass to the update methods an
update document of the form:
.. code-block:: python
{
<update operator>: { <field1>: <value1>, ... },
<update operator>: { <field2>: <value2>, ... },
...
}
.. include:: /includes/fact-update-set-create-fields.rst
.. tab::
:tabid: motor
To update a document, MongoDB provides
:manual:`update operators </reference/operator/update>` such
as :update:`$set` to modify field values.
To use the update operators, pass to the update methods an
update document of the form:
.. code-block:: python
{
<update operator>: { <field1>: <value1>, ... },
<update operator>: { <field2>: <value2>, ... },
...
}
.. include:: /includes/fact-update-set-create-fields.rst
.. tab::
:tabid: java-sync
To update a document, MongoDB provides
:manual:`update operators </reference/operator/update>` such
as :update:`$set` to modify field values.
The driver provides the `com.mongodb.client.model.Updates
<http://mongodb.github.io/mongo-java-driver/3.4/javadoc/com/mongodb/client/model/Updates.html>`__
class to facilitate the creation of update documents. For example:
.. code-block:: java
combine(set(<field1>, <value1>), set(<field2>, <value2>))
For a list of the update helpers, see
`com.mongodb.client.model.Updates
<http://mongodb.github.io/mongo-java-driver/3.4/javadoc/com/mongodb/client/model/Updates.html>`__.
.. include:: /includes/fact-update-set-create-fields.rst
.. tab::
:tabid: java-async
To update a document, MongoDB provides
:manual:`update operators </reference/operator/update>` such
as :update:`$set` to modify field values.
The driver provides the `com.mongodb.client.model.Updates
<http://mongodb.github.io/mongo-java-driver/3.4/javadoc/com/mongodb/client/model/Updates.html>`__
class to facilitate the creation of update documents. For example:
.. code-block:: java
combine(set(<field1>, <value1>), set(<field2>, <value2>))
For a list of the update helpers, see
`com.mongodb.client.model.Updates
<http://mongodb.github.io/mongo-java-driver/3.4/javadoc/com/mongodb/client/model/Updates.html>`__.
.. include:: /includes/fact-update-set-create-fields.rst
.. tab::
:tabid: kotlin-coroutine
To update a document, MongoDB provides
:ref:`update operators <update-operators>` such
as :update:`$set` to modify field values.
The driver provides the `com.mongodb.client.model.Updates
<{+java-api-docs+}/mongodb-driver-core/com/mongodb/client/model/Updates.html>`__
class to facilitate the creation of update documents. The
following code shows an update document that uses methods
from the ``Updates`` builder class:
.. code-block:: kotlin
combine(set(<field1>, <value1>), set(<field2>, <value2>))
.. include:: /includes/fact-update-set-create-fields.rst
.. tab::
:tabid: nodejs
To update a document, MongoDB provides
:manual:`update operators </reference/operator/update>` such
as :update:`$set` to modify field values.
To use the update operators, pass to the update methods an
update document of the form:
.. code-block:: javascript
{
<update operator>: { <field1>: <value1>, ... },
<update operator>: { <field2>: <value2>, ... },
...
}
.. include:: /includes/fact-update-set-create-fields.rst
.. tab::
:tabid: php
To update a document, MongoDB provides
:manual:`update operators </reference/operator/update>` such
as :update:`$set` to modify field values.
To use the update operators, pass to the update methods an
update document of the form:
.. code-block:: php
[
<update operator> => [ <field1> => <value1>, ... ],
<update operator> => [ <field2> => <value2>, ... ],
...
]
.. include:: /includes/fact-update-set-create-fields.rst
.. tab::
:tabid: perl
To update a document, MongoDB provides
:manual:`update operators </reference/operator/update>` such
as :update:`$set` to modify field values.
To use the update operators, pass to the update methods an
update document of the form:
.. code-block:: perl
{
<update operator> => { <field1> => <value1>, ... },
<update operator> => { <field2> => <value2>, ... },
...
}
.. include:: /includes/fact-update-set-create-fields.rst
.. tab::
:tabid: ruby
To update a document, MongoDB provides
:manual:`update operators </reference/operator/update>` such
as :update:`$set` to modify field values.
To use the update operators, pass to the update methods an
update document of the form:
.. code-block:: ruby
{
<update operator> => { <field1> => <value1>, ... },
<update operator> => { <field2> => <value2>, ... },
...
}
.. include:: /includes/fact-update-set-create-fields.rst
.. tab::
:tabid: csharp
To update a document, MongoDB provides
:manual:`update operators </reference/operator/update>` such
as :update:`$set` to modify field values.
To use the update operators, pass to the update methods an
update document of the form:
.. code-block:: c#
{
<update operator> => { <field1> => <value1>, ... },
<update operator> => { <field2> => <value2>, ... },
...
}
.. include:: /includes/fact-update-set-create-fields.rst
.. tab::
:tabid: scala
To update a document, MongoDB provides
:manual:`update operators </reference/operator/update>` such
as :update:`$set` to modify field values.
To use the update operators, pass to the update methods an
update document of the form:
.. code-block:: scala
(
set (<field1>, <value1>),
set (<field2>, <value2>),
...
)
.. include:: /includes/fact-update-set-create-fields.rst
.. tab::
:tabid: go
To update a document, MongoDB provides
:manual:`update operators </reference/operator/update>` such
as :update:`$set` to modify field values.
.. include:: /includes/fact-update-set-create-fields.rst
.. note::
MongoDB can accept an aggregation pipeline to specify the modifications to
make instead of an update document. For details, see the method reference
page.
.. _write-op-updateOne:
Update a Single Document
~~~~~~~~~~~~~~~~~~~~~~~~
.. tabs-drivers::
.. tab::
:tabid: shell
The following example uses the
:method:`db.collection.updateOne()` method on the
``inventory`` collection to update the *first* document where
``item`` equals ``"paper"``:
.. tab::
:tabid: compass
The following example demonstrates using |compass| to modify
a single document where ``item: paper`` in the ``inventory``
collection:
.. note::
This example uses the Compass
:ref:`Table View <documents-table-view>` to modify the
document. The editing process using the Compass
:ref:`List View <documents-list-view>` follows a very
similar approach.
For more information on the differences between Table View
and List View in Compass, refer to the
:ref:`Compass documentation <compass-view-documents>`.
.. tab::
:tabid: python
The following example uses the
:py:meth:`~pymongo.collection.Collection.update_one` method on
the ``inventory`` collection to update the *first* document
where ``item`` equals ``"paper"``:
.. tab::
:tabid: motor
The following example uses the
:py:meth:`~motor.motor_asyncio.AsyncIOMotorCollection.update_one`
method on the ``inventory`` collection to update the *first*
document where ``item`` equals ``"paper"``:
.. tab::
:tabid: java-sync
The following example uses the
com.mongodb.client.MongoCollection.updateOne_ method on the
``inventory`` collection to update the *first* document where
``item`` equals ``"paper"``:
.. tab::
:tabid: java-async
The following example uses the
`com.mongodb.reactivestreams.client.MongoCollection.updateOne
<http://mongodb.github.io/mongo-java-driver-reactivestreams/1.6/javadoc/com/mongodb/reactivestreams/client/MongoCollection.html#updateOne(org.bson.conversions.Bson,%20org.bson.conversions.Bson)>`_
on the ``inventory`` collection to update the *first* document
where ``item`` equals ``"paper"``:
.. tab::
:tabid: kotlin-coroutine
The following example uses the
`MongoCollection.updateOne()
<{+java-api-docs+}/mongodb-driver-kotlin-coroutine/mongodb-driver-kotlin-coroutine/com.mongodb.kotlin.client.coroutine/-mongo-collection/update-one.html>`__
method on the ``inventory`` collection to update the *first*
document where ``item`` equals ``"paper"``:
.. tab::
:tabid: nodejs
The following example uses the
:node-api:`Collection.updateOne() <Collection.html#updateOne>`
method on the ``inventory`` collection to update the *first*
document where ``item`` equals ``"paper"``:
.. tab::
:tabid: php
The following example uses the :phpmethod:`updateOne()
<phpmethod.MongoDB\\Collection::updateOne()>` method on the
``inventory`` collection to update the *first* document where
``item`` equals ``"paper"``:
.. tab::
:tabid: perl
The following example uses the
:perl-api:`update_one()<Collection#update_one>` method on the
``inventory`` collection to update the *first* document where
``item`` equals ``"paper"``:
.. tab::
:tabid: ruby
The following example uses the
:ruby-api:`update_one()<Collection.html#update_one-instance_method>`
method on the ``inventory`` collection to update the *first*
document where ``item`` equals ``"paper"``:
.. tab::
:tabid: scala
The following example uses the
:scala-api:`updateOne()<updateOne(filter:org.mongodb.scala.bson.conversions.Bson,update:org.mongodb.scala.bson.conversions.Bson,options:org.mongodb.scala.model.UpdateOptions):org.mongodb.scala.SingleObservable[org.mongodb.scala.result.UpdateResult]>`
method on the ``inventory`` collection to update the *first*
document where ``item`` equals ``"paper"``:
.. tab::
:tabid: csharp
The following example uses the
:csharp-api:`IMongoCollection.UpdateOne()
<M_MongoDB_Driver_IMongoCollection_1_UpdateOne>` method on the
``inventory`` collection to update the *first* document where
``item`` equals ``"paper"``:
.. tab::
:tabid: go
The following example uses the
:go-api:`Collection.UpdateOne<mongo#Collection.UpdateOne>` method
on the ``inventory`` collection to update the *first* document
where ``item`` equals ``"paper"``:
.. include:: /includes/driver-examples/driver-example-update-52.rst
.. _update-multiple-documents:
.. _write-op-updateMany:
Update Multiple Documents
~~~~~~~~~~~~~~~~~~~~~~~~~
.. tabs-drivers::
.. tab::
:tabid: compass
You can update only one document at a time in |compass|.
To update multiple documents, connect to your
MongoDB deployment from :binary:`~bin.mongosh` or a MongoDB driver
and follow the examples in this section for your preferred method.
.. tab::
:tabid: shell
The following example uses the
:method:`db.collection.updateMany()` method on the ``inventory``
collection to update all documents where ``qty`` is less than
``50``:
.. tab::
:tabid: python
The following example uses the
:py:meth:`~pymongo.collection.Collection.update_many` method on
the ``inventory`` collection to update all documents where ``qty``
is less than ``50``:
.. tab::
:tabid: motor
The following example uses the
:py:meth:`~motor.motor_asyncio.AsyncIOMotorCollection.update_many`
method on the ``inventory`` collection to update all documents
where ``qty`` is less than ``50``:
.. tab::
:tabid: java-sync
The following example uses the
com.mongodb.client.MongoCollection.updateMany_ method on
the ``inventory`` collection to update all documents where
``qty`` is less than ``50``:
.. tab::
:tabid: java-async
The following example uses the
`com.mongodb.reactivestreams.client.MongoCollection.updateMany
<http://mongodb.github.io/mongo-java-driver-reactivestreams/1.6/javadoc/com/mongodb/reactivestreams/client/MongoCollection.html#updateMany(org.bson.conversions.Bson,%20org.bson.conversions.Bson)>`_
method on the ``inventory`` collection to update all documents
where ``qty`` is less than ``50``:
.. tab::
:tabid: kotlin-coroutine
The following example uses the
`MongoCollection.updateMany()
<{+java-api-docs+}/mongodb-driver-kotlin-coroutine/mongodb-driver-kotlin-coroutine/com.mongodb.kotlin.client.coroutine/-mongo-collection/update-many.html>`__
method on the ``inventory`` collection to update all documents
where ``qty`` is less than ``50``:
.. tab::
:tabid: nodejs
The following example uses the :node-api:`Collection.updateMany()
<Collection.html#updateMany>` method on the ``inventory``
collection to update all documents where ``qty`` is less than
``50``:
.. tab::
:tabid: php
The following example uses the :phpmethod:`updateMany()
<phpmethod.MongoDB\\Collection::updateMany()>` method on the
``inventory`` collection to update all documents where ``qty`` is
less than ``50``:
.. tab::
:tabid: perl
The following example uses the
:perl-api:`update_many()<Collection#update_many>` method on the
``inventory`` collection to update all documents where ``qty`` is
less than ``50``:
.. tab::
:tabid: ruby
The following example uses the
:ruby-api:`update_many()<Collection.html#update_many-instance_method>`
method on the ``inventory`` collection to update all documents
where ``qty`` is less than ``50``:
.. tab::
:tabid: scala
The following example uses the
:scala-api:`updateMany()<updateMany(filter:org.mongodb.scala.bson.conversions.Bson,update:org.mongodb.scala.bson.conversions.Bson,options:org.mongodb.scala.model.UpdateOptions):org.mongodb.scala.SingleObservable[org.mongodb.scala.result.UpdateResult]>`
method on the ``inventory`` collection to update all documents
where ``qty`` is less than ``50``:
.. tab::
:tabid: csharp
The following example uses the
:csharp-api:`IMongoCollection.UpdateMany()
<M_MongoDB_Driver_IMongoCollection_1_UpdateMany>` method on the
``inventory`` collection to update all documents where ``qty`` is
less than ``50``:
.. tab::
:tabid: go
The following example uses the
:go-api:`Collection.UpdateMany<mongo#Collection.UpdateMany>`
method on the ``inventory`` collection to update all documents
where ``qty`` is less than ``50``:
.. include:: /includes/driver-examples/driver-example-update-53.rst
Replace a Document
~~~~~~~~~~~~~~~~~~
.. tabs-drivers::
.. tab::
:tabid: compass
You can't replace a document in |compass|.
To replace a document, connect to your
MongoDB deployment from :binary:`~bin.mongosh` or a MongoDB driver
and follow the examples in this section for your preferred method.
.. tab::
:tabid: shell
To replace the entire content of a document except for the ``_id``
field, pass an entirely new document as the second argument to
:method:`db.collection.replaceOne()`.
.. include:: /includes/fact-update-replace-example.rst
.. tab::
:tabid: python
To replace the entire content of a document except for the ``_id``
field, pass an entirely new document as the second argument to
:py:meth:`~pymongo.collection.Collection.replace_one`.
.. include:: /includes/fact-update-replace-example.rst
.. tab::
:tabid: motor
To replace the entire content of a document except for the ``_id``
field, pass an entirely new document as the second argument to
:py:meth:`~motor.motor_asyncio.AsyncIOMotorCollection.replace_one`.
.. include:: /includes/fact-update-replace-example.rst
.. tab::
:tabid: java-sync
To replace the entire content of a document except for the ``_id``
field, pass an entirely new document as the second argument to
com.mongodb.client.MongoCollection.replaceOne_.
.. include:: /includes/fact-update-replace-example.rst
.. tab::
:tabid: java-async
To replace the entire content of a document except for the ``_id``
field, pass an entirely new document as the second argument to
`com.mongodb.reactivestreams.client.MongoCollection.replaceOne
<http://mongodb.github.io/mongo-java-driver-reactivestreams/1.6/javadoc/com/mongodb/reactivestreams/client/MongoCollection.html#replaceOne(org.bson.conversions.Bson,%20TDocument)>`_.
.. include:: /includes/fact-update-replace-example.rst
.. tab::
:tabid: kotlin-coroutine
To replace the entire content of a document except for the ``_id``
field, pass an entirely new document as the second argument to
the `MongoCollection.replaceOne() <{+java-api-docs+}/mongodb-driver-kotlin-coroutine/mongodb-driver-kotlin-coroutine/com.mongodb.kotlin.client.coroutine/-mongo-collection/replace-one.html>`__ method.
.. include:: /includes/fact-update-replace-example.rst
.. tab::
:tabid: nodejs
To replace the entire content of a document except for the
``_id`` field, pass an entirely new document as the second
argument to
:node-api:`Collection.replaceOne() <Collection.html#replaceOne>`.
.. include:: /includes/fact-update-replace-example.rst
.. tab::
:tabid: php
To replace the entire content of a document except for the ``_id``
field, pass an entirely new document as the second argument to
:phpmethod:`replaceOne()
<phpmethod.MongoDB\\Collection::replaceOne()>`.
.. include:: /includes/fact-update-replace-example.rst
.. tab::
:tabid: perl
To replace the entire content of a document except for the ``_id``
field, pass an entirely new document as the second argument to
:perl-api:`replace_one()<Collection#replace_one>`.
.. include:: /includes/fact-update-replace-example.rst
.. tab::
:tabid: ruby
To replace the entire content of a document except for the ``_id``
field, pass an entirely new document as the second argument to
:ruby-api:`replace_one()<Collection.html#replace_one-instance_method>`.
.. include:: /includes/fact-update-replace-example.rst
.. tab::
:tabid: scala
To replace the entire content of a document except for the ``_id``
field, pass an entirely new document as the second argument to
:scala-api:`replaceOne()<replaceOne(filter:org.mongodb.scala.bson.conversions.Bson,replacement:TResult,options:org.mongodb.scala.model.UpdateOptions):org.mongodb.scala.SingleObservable[org.mongodb.scala.result.UpdateResult]>`
.. include:: /includes/fact-update-replace-example.rst
.. tab::
:tabid: csharp
To replace the entire content of a document except for the ``_id``
field, pass an entirely new document as the second argument to
:csharp-api:`IMongoCollection.ReplaceOne()
<M_MongoDB_Driver_IMongoCollection_1_ReplaceOne>`.
.. include:: /includes/fact-update-replace-example.rst
.. tab::
:tabid: go
To replace the entire content of a document except for the ``_id``
field, pass an entirely new document as the second argument to
:go-api:`Collection.ReplaceOne <mongo#Collection.ReplaceOne>`.
.. include:: /includes/fact-update-replace-example.rst
.. include:: /includes/driver-examples/driver-example-update-54.rst
.. _update-documents-atlas-ui:
Update a Document with {+atlas+}
------------------------------------
.. note::
You can update only one document at a time in the {+atlas+} UI.
To update multiple documents or replace an entire document,
connect to your Atlas deployment from :binary:`~bin.mongosh`
or a MongoDB driver and follow the examples on this page
for your preferred method.
The example in this section uses the :atlas:`sample supplies dataset
</sample-data/sample-supplies/>`. To learn how to load the sample dataset
into your {+atlas+} deployment, see :atlas:`Load Sample Data
</sample-data/#std-label-load-sample-data>`.
To update a document in {+atlas+}, follow these steps:
.. procedure::
:style: normal
.. step:: Navigate to the collection.
a. In the {+atlas+} UI, click :guilabel:`Database` in the sidebar.
#. For the database deployment that contains the sample data, click :guilabel:`Browse Collections`.
#. In the left navigation pane, select the ``sample_supplies`` database.
#. Select the ``sales`` collection.
.. step:: Specify a query filter document.
You can specify a :ref:`query filter document <document-query-filter>`
in the :guilabel:`Filter` field. A query filter document uses
:ref:`query operators <csfle-supported-query-operators>` to specify search conditions.
Copy the following query filter document into the
:guilabel:`Filter` search bar and click :guilabel:`Apply`:
.. code-block:: javascript
{ saleDate: { $gte: { $date: "2016-01-01T00:00-00:00" }, $lte: { $date: "2016-01-02T00:00-00:00" } } }
This query filter returns all documents in the ``sample_supplies.sales``
collection where ``saleDate`` is on or between January 1 and 2, 2016
UTC time.
.. step:: Edit a document.
To edit a document displayed in the query results, hover over the
document and click on the pencil icon. In the document editor, you can:
- Add a new field.
- Delete an existing field.
- Edit a field's name, value, or type.
- Revert a specific change.
For detailed instructions, see :atlas:`Create, View, Update, and Delete Documents
</atlas-ui/documents/#edit-one-document>`.