Vishnu S Kumar
Vishnu S Kumar

Reputation: 686

How to update an integer array element in Mongo DB using Spring Data?

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

Upvotes: 1

Views: 841

Answers (1)

Vishnu S Kumar
Vishnu S Kumar

Reputation: 686

 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

Upvotes: 1

Related Questions