- Stitch >
- MongoDB Atlas >
- Work With MongoDB
Update Documents in MongoDB¶
On this page
Overview¶
The code snippets on this page demonstrate how to update documents that are stored in a MongoDB collection. Update operations use query filters to specify which documents to update and update operators to describe the changes to apply to matching documents.
This page also covers several different update operators that you can use to update fields or update array values.
Data Model¶
The examples on this page use a collection named store.items
that
models various items available for purchase in an online store. Each
item has a name
, an inventory quantity
, and an array of customer
reviews
.
Snippet Setup¶
- Functions
- JavaScript SDK
- Android SDK
- iOS SDK
To use a code snippet in a function, you must first instantiate a MongoDB collection handle:
To use a code snippet in a JavaScript project, you must first do the following:
Import Stitch Dependencies¶
– or –
Instantiate a MongoDB Remote Collection Handle¶
To use a code snippet in an Android project, you must first do the following:
Set Up Your Project¶
Follow the steps in the Set up a MongoDB Mobile Project guide.
Note
For more details on setting up your Android app to use Stitch, refer to Build a Mobile App with Sync or Build a Local-Only Mobile App.
Import Stitch Dependencies¶
For CRUD operations on a remote MongoDB collection, you will use one
or more of the following import
statements:
To Sync documents between a remote and a local MongoDB instance, you will also need to import the following packages:
Important
If you use Sync, be sure to add .sync()
before every remote
database call in the examples below, as in the following:
To perform CRUD operations only on the local (on-device) database, import the following packages:
Instantiate a MongoDB Collection Handle¶
To use a code snippet in an iOS project, you must first do the following:
Set Up Your Project¶
Follow the steps in the Set up a MongoDB Mobile Project guide.
Import Stitch Dependencies¶
Initialize the MongoDB Stitch iOS SDK¶
Instantiate a MongoDB Collection Handle¶
Methods¶
Update a Single Document¶
You can update a single document using the
collection.updateOne()
action.
The following snippet updates the name
of a single document in the
items
collection from legos
to blocks
and adds a price
of 20.99
:
- Functions
- JavaScript SDK
- Android SDK
- iOS SDK
Update One or More Documents¶
You can update a single document using the
collection.updateMany()
action.
The following snippet updates all documents in the items
collection
by multiplying their quantity
values by 10
:
- Functions
- JavaScript SDK
- Android SDK
- iOS SDK
Options¶
Upsert Documents¶
If an update operation does not match any document in the collection,
you can automatically insert a single new document into the collection
that matches the update query by setting the upsert
option to
true
.
The following snippet updates a document in the items
collection
that has a name
of board game
by incrementing its quantity
by 5
. The upsert
option is enabled, so if no document has a
name
value of "board game"
then MongoDB inserts a new document
with the name
field set to "board game"
and the quantity
value set to 5
:
- Functions
- JavaScript SDK
- Android SDK
- iOS SDK
Field Update Operators¶
Set the Value of a Field¶
You can use the $set operator to set the value of a single field without affecting other fields in a document.
Rename a Field¶
You can use the $rename operator to change the name of a single field in a document.
Array Update Operators¶
Push an Element Into an Array¶
You can use the $push operator to add a value to the end of an array field.
Pop the Last Element out of an Array¶
You can use the $pop operator
to remove either the first or last element of an array field. Specify
-1
to remove the first element and 1
to remove the last element.