更新嵌入的特定记录文档

时间:2017-11-28 10:05:36

标签: mongodb-query spring-data-mongodb spring-webflux

这是我第一次使用MongoDB和Spring-data-mongo。

Company个对象

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

我想更新具有给定currentLocation

的车辆的plateNumber字段

显然,我被困在这里

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 个答案:

答案 0 :(得分:1)

这应该有效,将查询框架化以获取所需文档,然后创建Update对象并填充值,如下所示,

currentLocation

您可以尝试更新var _hero = [{ nick: "Mike", lvl: 500, x: 10, y: 10 }, { nick: "Mike", lvl: 500, x: 15, y: 15 }] let main = () => { _hero.forEach(function(_hero) { if (_hero.nick == "Mike") { console.log(_hero); } }); }; main();