- Stitch >
- MongoDB Atlas >
- Work With MongoDB
Delete Documents from MongoDB¶
On this page
Overview¶
The code snippets on this page demonstrate how to delete documents that are stored in a MongoDB collection. Delete operations use a query filter to specify which documents to delete.
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¶
Delete a Single Document¶
You can delete a single document from a collection using the
collection.deleteOne()
action.
The following snippet deletes one document in the items
collection
that has a name
value of legos
:
- Functions
- JavaScript SDK
- Android SDK
- iOS SDK
Delete One or More Documents¶
You can delete multiple items from a collection using the
collection.deleteMany()
action.
The following snippet deletes all documents in the items
collection
that do not have any reviews
:
- Functions
- JavaScript SDK
- Android SDK
- iOS SDK