Navigation

collection.deleteMany()

Important

The MongoDB Stitch SDK is deprecated. For guidance on migrating to MongoDB Realm, please see Migrate Your App from Stitch to Realm.

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​(Bson 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.