Navigation

collection.deleteOne()

Definition

collection.deleteOne()

Remove a single document from the collection based on a query filter.

Usage

Example

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

const query = { "name": "legos" };

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

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

const query = { "name": "legos" };

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

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

Document filterDoc = new Document().append("name", "legos");

final Task<RemoteDeleteResult> deleteTask = itemsCollection.deleteOne(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.deleteOne() action from the Swift/iOS SDK, use the RemoteMongoCollection.deleteOne() method.

let query : Document = ["name": "legos"];

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

Parameters

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

deleteOne(query)
Parameter Description

Query Filter

query: <document>

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

If multiple documents match the query, only the first document in sort order or natural order will be deleted.

The deleteOne() method has the following form:

deleteOne(query)
Parameter Description

Query Filter

query: <document>

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

If multiple documents match the query, only the first document in sort order or natural order will be deleted.

The deleteOne() method has the following form:

deleteOne(query)
Parameter Description

Query Filter

query: <document>

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

If multiple documents match the query, only the first document in sort order or natural order will be deleted.

The deleteOne() method has the following form:

deleteOne(query)
Parameter Description

Query Filter

query: <document>

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

If multiple documents match the query, only the first document in sort order or natural order will be deleted.

Return Value

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

Promise<result: document>

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

Promise<result: RemoteDeleteResult>

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

Task<RemoteDeleteResult result>

The RemoteMongoCollection.deleteOne() 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 a Document

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_grade with the following definition:

exports = function(grade) {
    var mongodb = context.services.get("mongodb-atlas");
    var coll = mongodb.db("test").collection("example");
    coll.deleteOne({"grade": grade})
      .then(result => {
         // result.deletedCount === 1
         return result;
      });
};

The function delete_grade deletes the document with the grade matching the argument.

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

stitchClient.executeFunction("delete_grade", 91)
    .then((result) => console.log(result));

This outputs:

{ "deletedCount": 1 }