我有一个包含产品列表的目录集合,我想通过添加另一种产品来更新目录,问题是在产品目录集合中它删除了现有产品并保留了添加的产品,而在产品集合中它们仍然存在
@Document(collection = "catalog")
public class Catalog {
@Id
String id;
String name;
@DBRef
List<Product> products;
}
@Document(collection = "product")
public class Product {
@Id
String id;
Integer price;
}
@Bean
CommandLineRunner init(MongoOperations mongoOperations) {
return args -> {
// step 1
List<Product> products1 = Arrays.asList(new Product().id("1").price(120), new Product().id("2").price(150));
for (Product product : products1) {
mongoOperations.save(product);
}
mongoOperations.save(new Catalog().id("55").name("Catalog 1").products(products1));
// step 2
List<Product> products2 = Arrays.asList(new Product().id("3").price(497));
for (Product product : products2) {
mongoOperations.save(product);
}
mongoOperations.save(new Catalog().id("55").name("Catalog 1").products(products2));
};
}