Reputation: 1180
This is my first time using MongoDB and Spring-data-mongo.
There is a Company
object
@Document
public class Company {
@Id
private String id;
private String name;
private String registrationNumber;
private List<Vehicle> vehicles;
}
public class Vehicle {
private String vehicleOwner;
@Indexed(unique = true)
private String plateNumber;
private Double speedLimit;
private GeoJsonPoint currentLocation;
}
I would like to update currentLocation
field for a vehicle with a given plateNumber
Obviousl, i'm stuck here
mongoTemplate.find(Query.query(Criteria.where("vehicles"))), how to go to `plateNumber` field? And how to update `currentLocation` field for that particular matching `Vehicle` for the `Company`
Upvotes: 0
Views: 47
Reputation: 656
This should work, frame the query to fetch the required document and then create an Update object and populate values as shown below,
Query query = new Query(Criteria.where("name").is("company_name")
.and("vehicles")
.elemMatch(Criteria.where("vehicleOwner").is("owner_name")));
Update update = new Update();
update.set("vehicles.$.plateNumber", "NEW NUMBER");
//You can use findAndModify or updateMulti based on your need.
mongoTemplate.updateFirst(query, update, Company.class);
You can try the same for updating currentLocation
.
Upvotes: 1