简体   繁体   English

给定产品 ID 列表,如何删除产品集合中的产品数据

[英]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: {}
}

Alternative选择

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.

相关问题 给定一个 id 列表,查询集合中不存在哪些 id 的最佳方法是什么? - Given a list of ids, what's the best way to query which ids do not exist in the collection? 如何永久删除forerunnerdb中的集合? - How do I permanently delete a collection in forerunnerdb? Mongoose - 我如何从一组对象 ID 的集合中找到 - Mongoose - How do i find in a collection from an array of object ids 如何按键从MongoDB集合中删除字段? - How do I delete a field from a MongoDB collection by key? 如何通过MEAN应用传递数据? 从产品页面到新页面,其中包含有关用户单击的特定产品的详细信息? - How do I pass data with my MEAN app? From a page of products to a new page with details about the specific product that the user clicked on? 如何在批量操作中无法插入/删除/更新文档的数组_ids? - How do I get array _ids of documents failed to insert/delete/update in bulk operation? 如何通过日期时间顺序从Mongo集合中获取ID的独特列表? - How to get the distinct list of ids from the Mongo Collection by datetime ordering? 如何列出集合中的所有文档,但显示单个属性? - How do I list all documents in a collection but show a single property? 如何使用 ID 数组查询 mongodb - How do I query mongodb with an array of IDs 如何 select 仅具有来自 ids 数组的 id 的产品。 MongoDB - How to select only products with ids from ids array. MongoDB
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM