gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 06:32:31 24 4
gpt4 key购买 nike

如何通过传递要删除的对象列表来从 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,
....
}]
}

});

最佳答案

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

  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);

关于java - Spring Mongo Query,删除集合匹配列表中的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45811861/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com