1

This is our document in MongoDB

We need to update the latitude coordinate (address.coord[0]) whose value is less than 50. How to build the query to do this using Spring Data. The following few snippets show the methods we tried.

The below one do the update but it removes coord-0(latitude) and coord-1(longitude)

Criteria c = where("address.coord").lt(value);
Update update = new Update().set("address.coord", value);
WriteResult result = mongodb.updateMulti(query(c), update, "restaurants");

The below one could't even able to find the documents matching the criteria

Criteria c = where("address.$.coord").lt(value);
Update update = new Update().set("address.$.coord", value);
WriteResult result = mongodb.updateMulti(query(c), update, "restaurants");

Thanks in advance for your valuable answers

5
  • 1
    Criteria c = where("address.coord.0").lt(value); Update update = new Update().set("address.coord.0", value); WriteResult result = mongodb.updateMulti(query(c), update, "restaurants"); Commented Sep 21, 2017 at 14:48
  • "address.$.coord" implies address is an array which is not the case hence the query which uses this fails. Commented Sep 21, 2017 at 14:53
  • @Veeram I tried that too, I got the same result as that of in my second code snippet. It updated the address.coord[0] but I lost address.cord[1] Commented Sep 21, 2017 at 15:03
  • 1
    Sorry didn't realize address was not a embedded array and coord is. Something like Criteria c = where("address.coord").lt(value); Update update = new Update().set("address.coord.$", value); WriteResult result = mongodb.updateMulti(query(c), update, "restaurants"); Commented Sep 21, 2017 at 15:05
  • @chridam it worked. Thanks, Chridam and Veeram Commented Sep 21, 2017 at 15:06

1 Answer 1

1
 Criteria c = where("address.coord.0").lt(value); 
 Update update = new Update().set("address.coord.0", value);
 WriteResult result = mongodb.updateMulti(query(c), update, "restaurants");

address.coord.0

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.