Programming Sharing

We share about programming.

Follow publication

Update document using MongoTemplate

Let’s learn how to update the document with MongoTemplate with an explanation of how it works.

Thành Trần
Programming Sharing
2 min readMar 18, 2021

Follow part one of the CRUD document using MongoTemplate series which shares about inserting documents, in this article, we will learn how to update documents by using MongoTemplate.

Update behavior

MongoTemplate provides a save method that allows updating documents. The diagram below shows how the save method work in basic.

How the mongotemplate save method works
  • At first, if the _id value was not provided the MongoDB will process the saving document as inserting a new document.
  • If the _id provided, MongoDB server will scan the collection and tries to a matching document with provided _id value. If a document matching the provided _id found, it will be replaced completely with the new document which passed as the argument. In contrast, a new document will be inserted.

Aside fromsave method, MongoTemplate also provides updateFirst, and updateMulti to allow update a new value of a document. These methods work a bit differently from the save method. These methods allow us to update specified fields without replacing the entire document. The updateFirst will be updating new values for the first document found by provided Query object. And the updateMulti will update all the documents found by the Query object, also known as the batch update.

Demo section

This demo is based on spring boot and using maven as the build tool.

First, add spring-boot-starter-data-mongodb to the pom.xml file. Please take a look at the pom.xml file.

Now to connect to MongoDB we must provide connection properties. To simplify, I combine required properties into a URI and put in the application.properties.

spring.data.mongodb.uri=mongodb://localhost:27017/demo

# Create a domain class that represents to collection structure

To simplify I write in a single class name DemoRunner which is a CommandLineRunner.

Conclusion

By now, I have shared how to updating documents with MongoTemplate and explained how the save method works as well and updateFirst and updateMulti method.

Reference

Demo Source Code

https://github.com/Programming-Sharing/spring-data-demo/tree/crud-document-using-mongotemplate-updating-documents

Call to action

I would like to promote you register Medium Membership to read all my articles and as well as other articles. If you feel interesting, you can register via the link below. I will earn a bit of money from Medium when you register through my link. Big thanks in advance.

https://tranthanhdeveloper.com/membership

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Written by Thành Trần

Software Engineer, Blogger at Programming Sharing. Enjoy technology, economics, and finance.

No responses yet

Write a response