If there are more than one conditions then use $elemMatch. The above two snippets works exactly the same because there is just one criterion i.e country is ‘usa’. Represents a user with some basic information like country, name, isGuest, and country. Let’s suppose we have the following data in our MongoDB posts collection stored in a very naive way (for simplicity).Įach post document has a title, author and a comments array field. One of my favourite mongoDB feature is that it stores data as documents in BSON (JSON in binary) because of which we can store array fields inside a document unlike a SQL based database where we would have to create another table for it. It is fast, scalable, dynamic and developer friendly. updateMulti Updates all documents that match the query. updateFirst Updates the first document that matches the query. save Update the whole object, if id is present, perform an update, else insert it.
Built several monoliths, microservices, serverless applications with different use cases using mongoDB. In Spring data MongoDB, you can use following methods to update documents.
I’ve worked with mongoDB for 3 years now. MongoDB - Update And Query Operators for Arrays