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.