Docs Menu
Docs Home
/ / /
Kotlin Sync Driver
/

Delete Documents

On this page

  • Overview
  • Sample Data
  • Delete Operations
  • Delete One Document
  • Delete Multiple Documents
  • Customize the Delete Operation
  • Return Value
  • API Documentation

In this guide, you can learn how to use the Kotlin Sync driver to remove documents from a MongoDB collection by performing delete operations.

A delete operation removes one or more documents from a MongoDB collection. You can perform a delete operation by using the deleteOne() or deleteMany() methods.

The examples in this guide use the sample_restaurants.restaurants collection from the Atlas sample datasets. To learn how to create a free MongoDB Atlas cluster and load the sample datasets, see the Get Started with Atlas guide.

The documents in this collection are modeled by the following Kotlin data class:

data class Restaurant(val name: String, val borough: String)

You can perform delete operations in MongoDB by using the following methods:

  • deleteOne(), which deletes the first document that matches the search criteria

  • deleteMany(), which deletes all documents that match the search criteria

Each delete method requires a query filter document, which specifies the search criteria that determine which documents to select for removal. To learn more about query filters, see the Specify a Query guide.

The following example uses the deleteOne() method to remove a document in which the value of the name field is "Happy Garden":

val filter = eq(Restaurant::name.name, "Happy Garden")
val result = collection.deleteOne(filter)

The following example uses the deleteMany() method to remove all documents in which the value of the borough field is "Brooklyn" and the value of the name field is "Starbucks":

val filter = and(
eq(Restaurant::borough.name, "Brooklyn"),
eq(Restaurant::name.name, "Starbucks")
)
val result = collection.deleteMany(filter)

The deleteOne() and deleteMany() methods optionally accept a DeleteOptions parameter, which represents options you can use to configure the delete operation. If you don't specify any options, the driver performs the delete operation with default settings.

The following table describes the setter methods that you can use to configure a DeleteOptions instance:

Method
Description
collation()
Specifies the kind of language collation to use when sorting results. For more information, see Collation in the MongoDB Server manual.
hint()
Specifies the index to use when matching documents. For more information, see the hint statement in the MongoDB Server manual.
hintString()
Specifies the index as a string to use when matching documents. For more information, see the hint statement in the MongoDB Server manual.
let()
Provides a map of parameter names and values to set top-level variables for the operation. Values must be constant or closed expressions that don't reference document fields. For more information, see the let statement in the MongoDB Server manual.
comment()
Sets a comment to attach to the operation. For more information, see the delete command fields guide in the MongoDB Server manual for more information.

The following code creates options and uses the comment() method to add a comment to the delete operation. Then, the example uses the deleteMany() method to delete all documents in the restaurants collection in which the value of the name field includes the string "Red".

val opts = DeleteOptions().comment("sample comment")
val filter = regex(Restaurant::name.name, "Red")
val result = collection.deleteOne(filter, opts)

Tip

If you use the the deleteOne() method in the preceding example instead of the deleteMany() method, the driver deletes only the first document that matches the query filter.

The deleteOne() and deleteMany() methods each return a DeleteResult instance. You can access the following information from a DeleteResult instance:

  • deletedCount, which indicates the number of documents deleted

  • wasAcknowledged(), which returns true if the server acknowledges the result

If the query filter does not match any documents, the driver doesn't delete any documents and the value of deletedCount is 0.

Note

If the wasAcknowledged() method returns false, trying to access the deletedCount property results in an InvalidOperation exception. The driver cannot determine these values if the server does not acknowledge the write operation.

To learn more about any of the methods or types discussed in this guide, see the following API documentation:

Back

Replace