0

i wanted to do a direct update to mongodb and setting someflag to either true or false for my use case. To be effecient i do not want to query all documents and set the someflag and save it back to db. i just want to directly update it on db just like when doing update on mongodb terminal.

Here is the sample document. NOTE: these documents can number from 1 ~ N so i need to handle efficiently big datas

{
  _id: 60db378d0abb980372f06fc1
  someid: 23cad24fc5d0290f7d5274f5
  somedata: some data of mine
  flag: false
}

Currently im doing an @Query method on my repository

@Query(value ="{someid: ?0}, {$set: {flag: false}}")
void updateFlag(String someid)

Using the above syntax, it doesnt work, i always get an exception message below;

Failed to instantiate void using constructor NO_CONSTRUCTOR with arguments

How do i perform a direct update effeciently without querying all those document and updating it back to db?

8
  • Do u have default constructor in your class? Commented Jul 13, 2021 at 4:09
  • which class? Repository or the Model that relates to this Repository? Commented Jul 13, 2021 at 4:32
  • U need to have default constructor in Model class Commented Jul 13, 2021 at 4:33
  • ohh i see, which constructor should i have, the one with all the class members as parameters? Commented Jul 13, 2021 at 4:34
  • No, you need to have no arguments constructor like in Person class, no argument constructor is Person(){} Commented Jul 13, 2021 at 4:35

1 Answer 1

1

Use the BulkOperations class (https://docs.spring.io/spring-data/mongodb/docs/current/api/org/springframework/data/mongodb/core/BulkOperations.html)

Sample codes: Spring Data Mongodb Bulk Operation Example

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.