Updating Object Data in MongoDB

Locating and updating Scala object data using Play Framework and MongoDB

Share:

Library Dependencies

Add MongoDB Scala driver library dependencies 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 and update the database name:

Server key points to where the database engine is running. In this case, the database is running on the same machine as the application.

Database key is the name of the database to be created and used by the application. In this case, the database is named sample.

Update Method

Usage Example

How Update Method Works

The method uses driver's replaceOne operation thread to remove the original document and insert the new one while keeping the document id.

Sample Code

LineDrop Play/Scala Web Application - Database


References

Artwork

  • Photo by LineDrop

Related Articles

Browse related articles