[英]MongoTemplate: remove list of elements returned from `find`
I am calling MongoTemplate.find(query, type)
which returns a List<T>
. 我正在调用MongoTemplate.find(query, type)
,它返回List<T>
。 My question, how do I remove all elements in the list from the DB Collection in one DB call. 我的问题是,如何通过一个数据库调用从数据库集合中删除列表中的所有元素。 I know I could create a Query
manually using id
if I know the structure of T
but what if I don't know what T
is. 我知道如果知道T
的结构,可以使用id
手动创建Query
,但是如果我不知道T
是什么,该怎么办。
MongoTemplate
has a remove
method that I can pass T
. MongoTemplate
有一个可以传递T
的remove
方法。 What I need is the equivalent removeAll(Collection<T> items, Class<T> type)
我需要的是等效的removeAll(Collection<T> items, Class<T> type)
EDIT per answer 每个答案编辑
I need to process each element and determine which ones to remove so I cannot simply pass the Query
to remove
. 我需要处理每个元素并确定要删除的元素,所以我不能简单地通过Query
将其remove
。
Instead of calling find(Query, Class) and then remove all returned documents (a Collection or one-by-one), you can simply call remove(Query, Class) . 无需调用find(Query,Class)然后删除所有返回的文档(Collection或一对一),只需调用remove(Query,Class)即可 。 It's much better that way since you don't have to return all documents to the client, but issue a remove query in the DB itself. 这种方法要好得多,因为您不必将所有文档都返回给客户端,而是可以在数据库本身中发出删除查询。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.