Navigation

collection.deleteMany()

Definition

collection.deleteMany()

Remove one or more documents from the collection based on a query filter.

Usage

Example

To call the collection.deleteMany() action from a Function, get a collection handle with database.collection() then call the handle’s deleteMany() method.

const query = { "reviews": { "$size": 0 } };

itemsCollection.deleteMany(query)
  .then(result => console.log(`Deleted ${result.deletedCount} item(s).`))
  .catch(err => console.error(`Delete failed with error: ${err}`))

To call the collection.deleteMany() action from a JavaScript SDK, use the RemoteMongoCollection.deleteMany() method.

const query = { "reviews": { "$size": 0 } };

itemsCollection.deleteMany(query)
  .then(result => console.log(`Deleted ${result.deletedCount} item(s).`))
  .catch(err => console.error(`Delete failed with error: ${err}`))

To call the collection.deleteMany() action from the Java/Android SDK, use the RemoteMongoCollection.deleteMany() method.

Document filterDoc = new Document().append("reviews", new Document().append("$size", 0));

final Task <RemoteDeleteResult> deleteTask = itemsCollection.deleteMany(filterDoc);
deleteTask.addOnCompleteListener(new OnCompleteListener <RemoteDeleteResult> () {
    @Override
    public void onComplete(@NonNull Task <RemoteDeleteResult> task) {
        if (task.isSuccessful()) {
            long numDeleted = task.getResult().getDeletedCount();
            Log.d("app", String.format("successfully deleted %d documents", numDeleted));
        } else {
            Log.e("app", "failed to delete document with: ", task.getException());
        }
    }
});

To call the collection.deleteMany() action from the Swift/iOS SDK, use the RemoteMongoCollection.deleteMany() method.

let query : Document = ["reviews": ["$size": 0] as Document];

itemsCollection?.deleteMany(query) { result in
    switch result {
    case .success(let result):
        print("Deleted \(result.deletedCount) item(s).");
    case .failure(let error):
        print("Delete failed with error: \(error)");
    }
}

Parameters

The collection.deleteMany() action has the following form:

deleteMany(query)
Parameter Description

Query Filter

query: <document>
Optional. A standard MongoDB query document that specifies which documents to delete. You can use most query selectors except for evaluation, geospatial, or bitwise selectors.

The deleteMany() method has the following form:

deleteMany(query)
Parameter Description

Query Filter

query: <document>
Optional. A standard MongoDB query document that specifies which documents to delete. You can use most query selectors except for evaluation, geospatial, or bitwise selectors.

The deleteMany() method has the following form:

deleteMany(query)
Parameter Description

Query Filter

query: <document>
Optional. A standard MongoDB query document that specifies which documents to delete. You can use most query selectors except for evaluation, geospatial, or bitwise selectors.

The deleteMany() method has the following form:

deleteMany(query)
Parameter Description

Query Filter

query: <document>
Optional. A standard MongoDB query document that specifies which documents to delete. You can use most query selectors except for evaluation, geospatial, or bitwise selectors.

Return Value

The collection.deleteMany() action returns a Promise that resolves to a document that describes the delete operation.

Promise<result: document>

The RemoteMongoCollection.deleteMany() method returns a Promise that resolves to a RemoteDeleteResult object that describes the delete operation.

Promise<result: RemoteDeleteResult>

The RemoteMongoCollection.deleteMany() method returns a Task that resolves to a RemoteDeleteResult object that describes the delete operation.

Task<RemoteDeleteResult result>

The RemoteMongoCollection.deleteMany() method returns a StitchResult enum that resolves to a RemoteDeleteResult object that describes the delete operation.

enum StitchResult {
  case success(result: RemoteDeleteResult)
  case failure(error: StitchError)
}
Value Description

Matched Count

result.deletedCount: <integer>
result.deletedCount: <integer>
result.deletedCount: <Long>
result.deletedCount: <Int>
The number of documents in the collection that were deleted by the delete operation.

Examples

The following examples assume that you have defined roles and permissions that allow the database operations on the test.example collection.

Deleting Multiple Documents

Assume the test.example collection contains the following documents:

{ "grade": 87, "_id": ObjectId("5a0c516ac7ecd33f1aea0915") }
{ "grade": 89, "_id": ObjectId("5a0c516ac7ecd33f1aea0916") }
{ "grade": 62, "_id": ObjectId("5a0c516ac7ecd33f1aea0917") }
{ "grade": 22, "_id": ObjectId("5a0c516ac7ecd33f1aea0918") }
{ "grade": 68, "_id": ObjectId("5a0c516ac7ecd33f1aea0919") }
{ "grade": 91, "_id": ObjectId("5a0c516ac7ecd33f1aea091A") }

MongoDB Stitch contains a function named delete_grades with the following definition:

exports = function(start, end) {
   var mongodb = context.services.get("mongodb-atlas");
   var coll = mongodb.db("test").collection("example");
   coll.deleteMany({"grade": {"$gt": start, "$lt": end}})
      .then(result => {
         // result.deletedCount === number of records that were deleted
         return result;
      });
};

The function delete_grades deletes documents with the grades between start and end.

Clients may call this function by referring to it by name. The following example calls the function from the Javascript SDK:

stitchClient.executeFunction("delete_grades", 80, 100)
    .then((result) => console.log(result));

This outputs:

{ "deletedCount": 3 }