java - Javers - 如何使用 Spring Data mongo 在 mongodb 中获取更改的集合字段的旧值?
问题描述
我已经浏览了许多链接,例如https://nullbeans.com/auditing-using-spring-boot-mongodb-and-javers/和Javers Comparing List In Order,但我真的很想了解文档的旧值已更新或旧值列表?
我开发了示例代码: 汽车
@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Car {
private String id;
private String brand;
private String year;
private String model;
private Long horsePower;
public Car(String brand, String year, String model, Long horsePower) {
super();
this.brand = brand;
this.year = year;
this.model = model;
this.horsePower = horsePower;
}
}
CarRepository.java
@JaversSpringDataAuditable
public interface CarRepository extends MongoRepository<Car, String>{
Car findByModel(String string);
List<Car> findByHorsePowerLessThan(int i);
}
SpringbootJaVersApplication.java
@SpringBootApplication
@Slf4j
public class SpringbootJaVersApplication implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(SpringbootJaVersApplication.class, args);
}
@Autowired
private CarRepository carRepository;
@Override
public void run(String... args) throws Exception {
//withOutJavers();
withJavers();
}
@Autowired
private Javers javers;
private void withJavers() {
QueryBuilder builder = QueryBuilder.byClass(Car.class).withSnapshotTypeUpdate();
List<CdoSnapshot> changes = javers.findSnapshots(builder.build());
log.info("Found the following updates: ");
for(CdoSnapshot change: changes) {
log.info("Car model snapshot: {}", change);
}
}
private void withOutJavers() {
// Clean the collection for our test
carRepository.deleteAll();
// create test data
// Car car1 = new Car("Ferrari", "2015", "488", 670);
// Car car2 = new Car("Fiat", "2012", "Abarth 595", 140);
// Car car3 = new Car("Fiat", "2007", "Abarth 500", 135);
Car car1 = Car.builder().brand("Ferrari").year("2015").horsePower(670L).model("488").build();
// Car car2 = Car.builder().brand("Fiat").year("2012").horsePower(595L).model("140").build();
// Car car3 = Car.builder().brand("Ferrari").year("2013").horsePower(135L).model("500").build();
carRepository.saveAll(Arrays.asList(car1));
// find a car by model
Car car488 = carRepository.findByModel("488");
// modify the car
car488.setHorsePower(800L);
carRepository.save(car488);
log.info("Car488: {}", car488);
// delete a car
// Car car500 = carRepository.findByModel("Abarth 500");
// carRepository.delete(car500);
// find cars with horse power less than 200
List<Car> cars = carRepository.findByHorsePowerLessThan(200);
// log the results
log.info("Found the following cars");
for (Car foundCar : cars) {
log.info(foundCar.toString());
}
}
}
快照记录 {
"_id" : ObjectId("5d0602e476e79c53f06f1d6b"),
"commitMetadata" : {
"author" : "unauthenticated",
"properties" : [
],
"commitDate" : "2019-06-16T14:20:44.465",
"commitDateInstant" : "2019-06-16T08:50:44.465Z",
"id" : NumberLong(1)
},
"globalId" : {
"valueObject" : "com.example.model.Car"
},
"state" : {
"horsePower" : NumberLong(670),
"year" : "2015",
"model" : "488",
"id" : "5d0602e476e79c53f06f1d6a",
"brand" : "Ferrari"
},
"changedProperties" : [
"horsePower",
"year",
"model",
"id",
"brand"
],
"type" : "INITIAL",
"version" : NumberLong(1),
"globalId_key" : "com.example.model.Car/"
}
// ----------------------------------------------
{
"_id" : ObjectId("5d0602e476e79c53f06f1d6d"),
"commitMetadata" : {
"author" : "unauthenticated",
"properties" : [
],
"commitDate" : "2019-06-16T14:20:44.574",
"commitDateInstant" : "2019-06-16T08:50:44.574Z",
"id" : NumberLong(2)
},
"globalId" : {
"valueObject" : "com.example.model.Car"
},
"state" : {
"horsePower" : NumberLong(800),
"year" : "2015",
"model" : "488",
"id" : "5d0602e476e79c53f06f1d6a",
"brand" : "Ferrari"
},
"changedProperties" : [
"horsePower"
],
"type" : "UPDATE",
"version" : NumberLong(2),
"globalId_key" : "com.example.model.Car/"
}
解决方案
推荐阅读
- r - R:将文档术语计数的数据框转换为文档术语矩阵(dtm)
- node.js - 带有 Node.js 和 Handlebars 的 Firebase 实时数据库
- jestjs - 如何为运行 Puppeteer 的 Jest 测试设置最大超时?
- javascript - 事件侦听器中的函数调用与函数引用
- javascript - 拖动按钮时并行修改div的高度
- facebook-graph-api - Facebook Graph API "(#279) 需要 read_page_mailboxes 权限来管理对象"
- ns2 - Ns2 802.11p 传播模型仿真参数
- google-sheets - 问:Google 表格 - 过滤相隔不到 24 个月的条目
- java - 使用 Firebase-UI (Android) 的 Firestore 聊天应用
- arrays - 在WebGL中将大数组传递给统一