On this page



Removes a single document from the collection based on the filter. The collection.deleteOne() method has the following parameter.

Parameter Type Description
filter document

The selection criteria for the removal. See query selectors for all available query selectors.

Specify an empty document { } to remove the first document returned from the collection.

Returns:A promise that resolves to a JSON object with the following fields:
  • deletedCount: the number of deleted documents


The following examples assume that the test.example namespace has been added to MongoDB in the Stitch admin console and appropriate rules have been set. See MongoDB Rules for more information.

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 ="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 }