In Chapter 2, Command-line Operations and Indexes, we had some recipes that explained various CRUD operations that we perform in MongoDB. There was one concept that we didn't cover that is, atomically finding and modifying documents. Modify consists of both update
and delete
operations. In this recipe, we will see find
and modify
operations in some detail and, in the next recipe, Implementing atomic counters in MongoDB, we will put them to use in implementing counters.
Refer to the Single node installation of MongoDB recipe in Chapter 1, Installing and Starting the MongoDB Server, and start a single instance of MongoDB. That is the only prerequisite for this recipe. Start a Mongo shell and connect to the started server.