2017-11-28 3 views
0

これは初めてのMongoDBとSpring-data-mongoです。特定のレコードを文書内に埋め込んで更新

Companyオブジェクトが、私は与えられたplateNumber

Obviouslと車両用currentLocationフィールドを更新したいと思います

@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; 
} 

、私はこれがここ

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` 

答えて

1

をすべきこだわってあります必要なドキュメントを取得するためにクエリをフレーム化し、次にUpdateオブジェクトを作成し、以下に示すように値を設定します。

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); 

currentLocationを更新することもできます。

関連する問題