Deleting Object Data from MongoDB

Delete an object stored in MongoDB collection using Play Framework and Scala.

Share:

Library Dependencies

Add the following line to build.sbt.

Configuration

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:

Delete Method

Code Example

Example: LineDrop Play/Scala Web Application - Database Utility

Usage Example

How Delete Method Works

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.


References

Artwork

Related Articles

Browse related articles