Mongodb按照时间要求删除数据

mongodb的安装母上cmd 然后输入 C:\Windows\System32\cmd.exe

mongodb mongodb://172.26.226.17:2001

你绑定的ip地址和端口 连接成功后提示以下信息

db://172.26.226.171:20001
MongoDB shell version v4.4.2
connecting to: mongodb://172.26.226.171:20001/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("a530785d-fd9e-4ee1-bf33-69fbab3161f0") }
MongoDB server version: 4.4.2
---
The server generated these startup warnings when booting:
        2021-01-16T18:11:32.028+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
---
---
        Enable MongoDB's free cloud-based monitoring service, which will then receive and display
        metrics about your deployment (disk utilization, CPU, operation statistics, etc).

        The monitoring data will be available on a MongoDB website with a unique URL accessible to you
        and anyone you share the URL with. MongoDB may use this information to make product
        improvements and to suggest MongoDB products and deployment options to you.

        To enable free monitoring, run the following command: db.enableFreeMonitoring()
        To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---

然后开始使用 你的数据库和索引 

然后开始查找和删除。

 

> use Farmzai_Dev
switched to db Farmzai_Dev
> db.Device_Data.find().limit(1)
{ "_id" : NumberLong(1047459), "Name" : "Air Pressure", "Value" : 101093, "Unit" : "Pa", "CollectTime" : ISODate("2019-11-11T09:16:04Z"), "TrueCollectTime" : null, "MeasurementId" : 4, "MeasurementTypeId" : 4, "DeviceId" : 10188, "DeviceTypeId" : 2, "Brand_Id" : 10008, "FarmId" : 412, "FarmRoomId" : 551, "Model_Id" : 10017, "OrgId" : 10163, "CreateTime" : ISODate("2019-11-11T09:52:30Z") }
> db.Device_Data.find({"CollectTime":{$gt:ISODate("2021-01-14 00:00:00"),$lte:ISODate("2021-01-15 23:59:59")}}).count()
12665
> db.Device_Data.remove({"CollectTime":{$gt:ISODate("2021-01-14 00:00:00"),$lte:ISODate("2021-01-15 23:59:59")}})
WriteResult({ "nRemoved" : 12665 })
> db.Device_Data.find({"CollectTime":{$gt:ISODate("2021-01-14 00:00:00"),$lte:ISODate("2021-01-15 23:59:59")}}).count()
0
>
>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
在Spring Boot中设置MongoDB的自动过期时间并自动删除数据可以通过设置TTL(Time To Live)索引来实现。 TTL索引是一种设置文档过期时间的方法,它允许我们在MongoDB中设置一个特定的时间,在这个时间之后,该文档将被自动删除。 首先,我们需要在MongoDB的集合中创建一个TTL索引。假设我们想要将过期时间设置为24小时,我们可以执行以下操作: 1. 在对应的MongoDB集合中创建TTL索引,可以使用以下命令: ```java db.myCollection.createIndex( { "createdAt": 1 }, { expireAfterSeconds: 86400 }) ``` 以上命令中的`myCollection`是集合的名称,`createdAt`是字段的名称,`expireAfterSeconds`是过期时间。 2. 在Spring Boot项目中使用MongoDB的Java驱动程序,添加以下依赖项: ```java <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency> ``` 3. 创建一个实体类来映射MongoDB的文档,例如: ```java @Document(collection = "myCollection") public class MyDocument { @Id private String id; private String data; private Date createdAt; // getters and setters } ``` 4. 在Spring Boot的配置类中,使用@EnableMongoRepositories注解开启MongoDB的Repository功能。 5. 创建一个MongoDB的Repository接口,通过继承MongoRepository接口,例如: ```java public interface MyDocumentRepository extends MongoRepository<MyDocument, String> { } ``` 6. 在业务逻辑中使用MyDocumentRepository操作数据库,例如: ```java @Autowired private MyDocumentRepository myDocumentRepository; public void saveDocument(MyDocument document) { document.setCreatedAt(new Date()); myDocumentRepository.save(document); } ``` 通过以上步骤的设置,当往集合中插入文档时,MongoDB会自动设置并计算过期时间,并在过期时间到达后自动删除该文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值