使用 Springdata mongo Query 删除集合中的所有文档 [英] Delete all documents in a collection with Springdata mongo Query

查看:77
本文介绍了使用 Springdata mongo Query 删除集合中的所有文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个删除方法来删除集合中的所有文档.我在 Spring Data 中使用 mongo db.这可以在 Mongo shell 中使用 db.myCollection.remove({}) 来完成.但是我想在我的数据访问层中编写一个方法来做到这一点.我没有在我的 Dao 类中使用 MongodbTemplate.我想知道如何使用 Query 执行此操作.

I want to implement a delete method which deletes all the documents in a collection. I am using mongo db with Spring Data.This could be done using db.myCollection.remove({}) in Mongo shell. But I want to write a method in my data access layer to do this.I am not using MongodbTemplate in my Dao class. I want to know how can I do this with Query.

Query query = new Query();

谁能告诉我我该怎么做.

Could anybody please tell me how can I do it.

推荐答案

使用 MongoRepository 的 deleteAll().利用幕后的 mongoTemplate 调用 remove 方法.

Use MongoRepository's deleteAll(). Utilizes mongoTemplate behind the scene to call the remove method.

来自调用方法 someRepository.deleteAll()

丢弃收集可能很有​​效,正如其他答案所指出的那样.为此,您需要直接使用 MongoTemplate 并调用 dropCollection 带有实体类或集合名称.

Drop collection may be efficient as other answer has noted. For that you will need to use MongoTemplate directly and call dropCollection with entity class or collection name.

这篇关于使用 Springdata mongo Query 删除集合中的所有文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆