[英]How do I delete productData in products collection given a list of product ids
I have a document which has a field that I want to delete given a list of ids.我有一个文档,其中有一个字段,我想在给定 ID 列表的情况下删除该字段。 Here is one of the objects in the collection named products: Given a list of productIds delete the productData fields for those productIds.
这是名为 products 的集合中的对象之一:给定 productId 列表,删除这些 productId 的 productData 字段。 The method signature is like this
方法签名是这样的
//return productIds whose productData field has been deleted
public List<ProductId> deleteProductDataForIds(List<ProductId> productIds);
The productIds are an array similar to that shown below and are represented as a ProductId class and the product JSON is as shown below with additional fields omitted for brevity: productIds 是一个类似于下面所示的数组,表示为 ProductId class,产品 JSON 如下所示,为简洁起见省略了其他字段:
{
"_id" : {
"productId" : P202,
"categoryId" : 111
},
"productData" : {
"description": "Paco Rabane Fragrance",
"productSku": "11222-RTTTT"
},
"productName: "Paco Rabanne",
"qty": 1222222,
... other fields omitted for brevity
}
I want to use Spring Data Mongo, to delete the productData given a list of _id values.我想使用 Spring Data Mongo,删除给定 _id 值列表的 productData。 For example, given the list of productIds below, delete all the related productData.
例如,给定下面的 productIds 列表,删除所有相关的 productData。
[
{
"_id": {
"productId": 112,
"category" : "444""
},
"_id": {
"productId": 132,
"category" : "445""
}
]
I put together some code to attempt the deletion, but I'm stuck with how to add the delete portion.我整理了一些代码来尝试删除,但我对如何添加删除部分感到困惑。
var productId = new ProductId(1122, 122);
BulkOperations bulkOps = mongoTemplate.bulkOps(BulkOperations.BulkMode.UNORDERED,
Product.class, "products");
List<Pair<Query, Update>> updates = new ArrayList<>();
// This is not where I'm stuck adding code to delete productData
products.forEach(product -> updates.add(Pair.of(where("productId",
product.getProductId()),set(product))));
log.info("Writing to database {} ",updates );
}
//Helper method for add clause
private static Query where(String field, Object value) {
return new Query().addCriteria(Criteria.where(field).is(value));
}
I'm not sure the updateMulti can be used.我不确定是否可以使用 updateMulti。 I'm able to use it to update the products, but unsure how to delete the productData for a list of product ids.
我可以使用它来更新产品,但不确定如何删除产品 ID 列表的产品数据。
You need to use MongoTemplate.remove(...)
method:您需要使用
MongoTemplate.remove(...)
方法:
Note: It returns DeleteResult
which allows you verify deleted documents ( DeleteResult.getDeletedCount()
)注意:它返回
DeleteResult
允许您验证已删除的文档( DeleteResult.getDeletedCount()
)
public DeleteResult deleteProductDataForIds(List<ProductId> productIds) {
Query query = Query.criteria(Criteria.where("_id").in(productIds));
return mongoTemplate.remove(query, Product.class);
//Query: { "_id" : { "$in" : [ { "$java" : ProductId@6d969330 }, { "$java" : ProductId@4861cca9 } ] } }, Fields: {}, Sort: {}
}
You can use Criteria.orOperator operator and remove by key pairs.您可以使用Criteria.orOperator运算符并通过密钥对删除。
public DeleteResult deleteProductDataForIds(List<ProductId> productIds) {
Query query = Query.query(new Criteria().orOperator(productIds.stream()
.map(p -> Criteria.where("_id.productId").is(p.getProductId()).and("_id.categoryId").is(p.getCategoryId()))
.collect(Collectors.toList()))
);
return mongoTemplate.remove(query, Product.class);
//Query: { "$or" : [{ "_id.productId" : 112, "_id.categoryId" : "445"}, { "_id.productId" : 132, "_id.categoryId" : "444"}]}, Fields: {}, Sort: {}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.