Spring Mongo Query,删除集合匹配列表中的对象

时间:2017-08-22 07:51:38

标签: java spring mongodb

如何通过传递要删除的对象列表从mongo集合中删除对象:我使用的是spring和mongo存储库,下面是我的代码:

 public void removeDocuments(List<PayloadLogs> listLogs){

     String collectionName = mongoTemplate.getCollectionName(Logs.class);
     Query removeQuery = Query.query(Criteria.where("typeHash").in(listLogs));

// this does not removes the documents.
    this.mongoTemplate.findAllAndRemove(removeQuery, PayloadLogs.class, collectionName);
}

查询日志:

db.getCollection('payloadLogs').find({
"creativeHash": {
    "$in": [{
        "creativeHash": "21540209fa87504bbbb0dd173c41d742",
        "lastAccessedAt": null,
        ....
    }]
}

});

1 个答案:

答案 0 :(得分:1)

我认为这会有所帮助,将您的ID添加到列表中,并将按ID删除

  BasicDBObject query = new BasicDBObject();
  List<Integer> list = new ArrayList<Integer>();
  list.add(10004);
  list.add(10005);
  query.put("_id", new BasicDBObject("$in", list));
  collection.remove(query);