Han Wang
Han Wang

Reputation: 123

how to use spring data mongodb repository query annotation for updating

I am using mongodb for a java application and trying to use spring data repository to update the document. I use the @Query annotation like this: @Query("{ 'username' : ?0 }, { $set : { 'age' : ?1}}") void updateAgeByUserName(String username, int age);

But this doesn't work. I know the save will update the whole thing but I just want to update the age field using update query. How can I do this?

Appreciate any help.

Upvotes: 2

Views: 2143

Answers (1)

Miguel Cartagena
Miguel Cartagena

Reputation: 2606

Spring-data is a framework for general purpose that provides a very basic support to CRUD operations. If you need complex operations, like update using any $operator, you'll need to implement a custom repository to it.

Upvotes: 5

Related Questions