java mongodb 批量删除_MONGODB删除/新增/更改大量记录的方法

@Indexed(expireAfterSeconds=180)

private Date deletedAt;

以上代码,如果字段deletedAt有值,那么将在180秒后被MONGODB删除,如果没值不会被删除。

批量新增,小批量更新,防止读取超时

private  void insertAll(List list) {

if (null != list) {

int total = list.size();

int count = (total + 50000 -1) / 50000;

for (int i = 0; i 

int toIndex = ((i +1) * 50000 > total) ? total : ((i +1) * 50000);

log.info("toIndex = " + toIndex);

mongoTemplate1.insertAll(list.subList(i * 50000, toIndex));

}

}

}

批量更改

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.mongodb.core.MongoTemplate;

import org.springframework.data.mongodb.core.query.Criteria;

import org.springframework.data.mongodb.core.query.Query;

import org.springframework.data.mongodb.core.query.Update;

import com.tcl.project7.boss.gameapplication.yearendactivities.bigwheelgame.valueobject.SingleUseRedeemCode;

public class SingleUseRedeemCodeRepositoryImpl implements SingleUseRedeemCodeRepositoryCustom{

@Autowired

private MongoTemplate mongoTemplate1;

public void batchUpdateSingleUseRedeemCodeList(String bigWheelGameAwardId) {

Query query = new Query();

query.addCriteria(Criteria.where("bigwheelgameawardid").is(bigWheelGameAwardId));

mongoTemplate1.updateMulti(

query,

new Update().set("bigwheelgameawardid", "-1")

.set("deletedat", new Date()),

SingleUseRedeemCode.class);

}

}

Expire Data from Collections by Setting TTL¶

New in version 2.2.

This document provides an introduction to MongoDB’s “time to live” or TTL collection feature. TTL collections make it possible to store data in MongoDB and have the mongod automatically remove data after a specified number of seconds or at a specific clock time.

Data expiration is useful for some classes of information, including machine generated event data, logs, and session information that only need to persist for a limited period of time.

A special TTL index property supports the implementation of TTL collections. The TTL feature relies on a background thread in mongod that reads the date-typed values in the index and removes expired documentsfrom the collection.

Procedures

To create a TTL index, use the db.collection.createIndex() method with theexpireAfterSeconds option on a field whose value is either a date or an array that contains date values.

NOTE

The TTL index is a single field index. Compound indexes do not support the TTL property. For more information on TTL indexes, see TTL Indexes.

Expire Documents after a Specified Number of Seconds

To expire data after a specified number of seconds has passed since the indexed field, create a TTL index on a field that holds values of BSON date type or an array of BSON date-typed objects and specify a positive non-zero value in the expireAfterSeconds field. A document will expire when the number of seconds in the expireAfterSeconds field has passed since the time specified in its indexed field. [1]

For example, the following operation creates an index on the log_events collection’s createdAt field and specifies the expireAfterSeconds value of 3600 to set the expiration time to be one hour after the time specified by createdAt.

db.log_events.createIndex( { "createdAt": 1 }, { expireAfterSeconds: 3600 } )

When adding documents to the log_events collection, set the createdAt field to the current time:

db.log_events.insert( { "createdAt": new Date(), "logEvent": 2, "logMessage": "Success!" } )

MongoDB will automatically delete documents from the log_events collection when the document’screatedAt value [1] is older than the number of seconds specified in expireAfterSeconds.[1](1, 2) If the field contains an array of BSON date-typed objects, data expires if at least one of BSON date-typed object is older than the number of seconds specified in expireAfterSeconds.

SEE ALSO

Expire Documents at a Specific Clock Time

To expire documents at a specific clock time, begin by creating a TTL index on a field that holds values of BSON date type or an array of BSON date-typed objects and specify an expireAfterSeconds value of0. For each document in the collection, set the indexed date field to a value corresponding to the time the document should expire. If the indexed date field contains a date in the past, MongoDB considers the document expired.

For example, the following operation creates an index on the log_events collection’s expireAt field and specifies the expireAfterSeconds value of 0:

db.log_events.createIndex( { "expireAt": 1 }, { expireAfterSeconds: 0 } )

For each document, set the value of expireAt to correspond to the time the document should expire. For instance, the following insert() operation adds a document that should expire at July 22, 201314:00:00.

db.log_events.insert( { "expireAt": new Date('July 22, 2013 14:00:00'), "logEvent": 2, "logMessage": "Success!" } )

MongoDB will automatically delete documents from the log_events collection when the documents’expireAt value is older than the number of seconds specified in expireAfterSeconds, i.e. 0 seconds older in this case. As such, the data expires at the specified expireAt value.

posted on 2015-12-11 15:03 paulwong 阅读(1225) 评论(0)  编辑  收藏 所属分类: MONGODB

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/304026.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Redis 通过 RDB 方式进行数据备份与还原

Redis 通过 RDB 方式进行数据备份与还原Intro有的时候我们需要对 Redis 的数据进行迁移,今天介绍一下通过 RDB(快照)文件进行 Redis 数据的备份和还原Redis 持久化Redis 的数据持久化有两种机制,一种是 RDB(Redis Database)&#…

java proguard 使用_一步步教你使用Proguard混淆Java源代码

ava代码很容易被反编译,以下使用proguard来保护我们的代码proguard选项很多,容易迷糊,现在就把我的配置写下来(实际使用中),以供参考2.准备好你的jar包,我在这里举例叫做test.jar。3.解压proguard,执行 bin…

vi的使用

vi编辑器是所有Unix及Linux系统下标准的编辑器,它的强大不逊色于任何最新的文本编辑器,这里只是简单地介绍一下它的用法和一小部分指令。由于 对Unix及Linux系统的任何版本,vi编辑器是完全相同的,因此您可以在其他任何介绍vi的地方…

稳定匹配问题——稳定婚姻算法设计

图片源自&#xff1a;美剧《How I met your mother》****本代码带有详细的注释&#xff0c;并在控制台输出时详细地说明了算法的过程&#xff0c;非常有助于新手理解稳定匹配问题和稳定婚姻算法的设计思路。****#include <iostream>using namespace std;bool finish_or_n…

如果诸葛亮用C#写出师表...

❝看到一篇18年的文章 "C版《出师表》"&#xff0c;站长觉得挺有意思的&#xff0c;就用C# 控制台也实现了一遍&#xff0c;技术上没啥难度&#xff0c;但复制代码费了1、2个小时&#xff0c;纯粹无聊写着玩&#xff0c;看者别在意枚举、类名、变量中文命名&#xff…

简单的作好服务器安全的几个步骤

一.停掉Guest 帐号 在计算机管理的用户里面把guest帐号停用掉&#xff0c;任何时候都不允许guest帐号登陆系统。为了保险 起见&#xff0c;最好给guest 加一个复杂的密码&#xff0c;你可以打开记事本&#xff0c;在里面输入一串包含特殊字符,数 字&#xff0c;字母的长字符…

java蛮力法背包问题_[算法课]五种蛮力法解决01背包问题

文章目录注明&#xff1a;题目要求只能使用蛮力法算法标签&#xff1a;全排列&#xff0c;枚举&#xff0c;二进制&#xff0c;dfs&#xff0c;数组题目简介思路AC代码方法一&#xff1a;字符串蛮力方法二&#xff1a;二进制枚举方法三&#xff1a;DFS三.&#xff12;闫老板思考…

这16个数据可视化案例,惊艳了全球数据行业

数据可视化可以帮你更容易的解释趋势和统计数据。数据是非常强大的。当然&#xff0c;如果你能真正理解它想告诉你的内容&#xff0c;那它的强大之处就更能体现出来了。通过观察数字和统计数据的转换以获得清晰的结论并不是一件容易的事。必须用一个合乎逻辑的、易于理解的方式…

asp.net core 自定义 Content-Type

asp.net core 实现支持自定义 Content-TypeIntro我们最近有一个原本是内网的服务要上公网&#xff0c;在公网上有一层 Cloudflare 作为网站的公网流量提供者&#xff0c;CloudFlare 会有一层防火墙拦截掉一些非法的请求&#xff0c;我们有一些 API 会提交一些 html 内容&#x…

left outer join 和 right outer join 和 join 的区别

举个例子你就能知道了&#xff01;A表(a1,b1,c1) B表(a2,b2)a1 b1 c1 a2 b201 数学 95 01 张三02 语文 90 02 李四03 英语 80 04 王五select A.*,B.* from A inner join B on(A.a1B.a2)结果是&#xff1a;a1 b1 c1 …

liunx+java+jar+运行_Linux后台运行java的jar包

Linux 运行jar包命令如下&#xff1a;方式一java -jar shareniu.jar特点&#xff1a;当前ssh窗口被锁定&#xff0c;可按CTRL C打断程序运行&#xff0c;或直接关闭窗口&#xff0c;程序退出那如何让窗口不锁定&#xff1f;方式二java -jar shareniu.jar &&代表在后台…

如何优雅的移植JavaScript组件到Blazor

Blazor作为一个新兴的交互式 Web UI 的框架&#xff0c;有其自身的优缺点&#xff0c;如果现有的 JavaScript 组件能移植到 Blazor&#xff0c;无疑让 Blazor 如虎添翼&#xff0c;本文就介绍一下自己在开发 BulmaRazor 组件库的时&#xff0c;封装现有的 JavaScript 组件的方法…

把握人工智能命脉的有效方法

最近广州的天气老是变幻无常&#xff0c;往往今天还热得要命第二天就寒风瑟瑟&#xff08;如下图&#xff09;&#xff0c;让小天甚是怀念每天艳阳高照的夏天&#xff0c;虽然热了点但好歹不用担心猝不及防地收到寒风暴雨黄色预警。说到夏天&#xff0c;不得不提一下1956年的那…

取消智能菜单功能

如果你用惯了 Office 97&#xff0c;再使用 Office 2000 的话&#xff0c;可能对 Office 2000 里的菜单显示方式会感到不习惯&#xff0c;因为它不会一次显示出来&#xff0c;还要按一下下面的箭头&#xff0c;或是单击菜单的最上面&#xff0c;它才会一次通通显示出来。 这是 …

微软的焦虑?想多了!从.NET6 Preview2到大厂招聘,起飞

看了篇文章叫《从.NET看微软的焦虑》&#xff0c;这里忍不住先吐槽一下&#xff0c;看完不仅毫无收获&#xff0c;而且有一种先起个夺眼球的标题&#xff0c;然后再东拼西凑找证据。讲真的&#xff0c;微软市值基本上等于“阿里腾讯百度”三者之和&#xff0c;居然还焦虑的无法…

TED演讲:区块链将如何改变世界?看完太震撼了!

区块链是什么&#xff1f;如果你不知道&#xff0c;你应该了解&#xff1b;如果你知道&#xff0c;有可能你仍需要了解一些它工作原理。唐泰普斯科特在此使这改变世界、建立信任的科技变得简明易懂。他表示&#xff0c;这就是第二代互联网&#xff0c;将有可能改变我们的金钱、…

getopt()简介

函数getopt()用来分析命令行参数&#xff0c;其函数原型和相关变量声明如下&#xff1a; #include extern char *optarg; extern int optind, // 初始化值为1&#xff0c;下一次调用getopt时&#xff0c;从optind存储的位置重新开始检查选项&#xff0c;也就是从下一个-的选项…

re管理器Java_自定义布局管理器-FormLayout

第二部分&#xff1a;自定义布局管理器在java.awt包与javax.swing包下有许多现成的布局类&#xff0c;比如BorderLayout、FlowLayout&#xff0c;还有较为复杂的、用于精确定位的布局类GridBagLayout、SpringLayout等。起初我刚刚从事gooey时(06年中)&#xff0c;企图依靠JDK自…

如何看待 70% 的程序员,缺乏数据结构和算法知识?

金三银四来了&#xff0c;各大厂动静不小&#xff0c;都在储备人才&#xff0c;绝对是程序员面试的黄金时间了&#xff0c;不少同学也在后台反馈面试中遇到的一些问题&#xff0c;所以今天想跟大家说说算法。说起算法&#xff0c;那大厂面试是绝对必考的&#xff0c;可以说是一…

Sorry,关注这些 IT 技术类公众号,真的可以为所欲为

工作和生活节奏超快的今天&#xff0c;想要不断提升自我&#xff0c;碎片化阅读学习是你最佳的选择&#xff0c;如果你已经有了一颗学习的心&#xff0c;却苦于不知道从哪里学习&#xff0c;那么&#xff0c;这些学习的工具和途径就很重要了。今天为你推荐一些 IT技术领域的微信…