Delete an object stored in MongoDB collection using Play Framework and Scala.
Add the following line to build.sbt.
Storing database connection parameters in the application configuration file helps to avoid “hard-coding” the variables and makes it easy to change the server location as well as the name of the database. Add the following entry to conf/application.conf:
The delete method locates a document based on a search criteria and deletes it.
The method accepts the search key, search value, and the collection name. It opens a client connection to the database server, accesses the database, and the collection.
The method uses the driver’s deleteOne method which is a threaded operation. An observer is registered with the tread to report back once the operation is completed. Client is explicitly closed when the thread is finished.
Browse related articles