<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    paulwong

    MONGODB刪除/新增/更改大量記錄的方法

    MONGODB中,由于刪除大量記錄會十分耗時,一般推薦由MONGODB自己在后臺處理,只需在某個字段設(shè)一個索引的標簽即可。

    @Indexed(expireAfterSeconds=180)
    private Date deletedAt;

    以上代碼,如果字段deletedAt有值,那么將在180秒后被MONGODB刪除,如果沒值不會被刪除。

    批量新增,小批量更新,防止讀取超時
    private <T> void insertAll(List<T> list) {
            if (null != list) {
                int total = list.size();
                int count = (total + 50000 -1) / 50000;
                for (int i = 0; i < count; 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](12) 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

    $currentDate operator

    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 閱讀(1800) 評論(0)  編輯  收藏 所屬分類: MONGODB

    主站蜘蛛池模板: 亚洲熟妇无码AV| 亚洲日韩精品无码AV海量| 手机永久免费的AV在线电影网| 好吊妞在线新免费视频| 亚洲一区二区免费视频| 一二三四免费观看在线电影| 亚洲w码欧洲s码免费| 最近中文字幕免费mv视频7| 亚洲精品美女久久7777777| 国产无遮挡色视频免费视频| 亚洲精品无码你懂的| 免费看国产曰批40分钟| 免费在线观看一区| 久久久久亚洲精品天堂久久久久久| free哆拍拍免费永久视频| 国产亚洲av人片在线观看| 亚洲一区免费观看| 亚洲1234区乱码| 国产免费小视频在线观看| 久久WWW免费人成—看片| 久久香蕉国产线看观看亚洲片| 4455永久在线观免费看| 亚洲综合av一区二区三区| 九月婷婷亚洲综合在线| 成人无码WWW免费视频| 亚洲成人免费电影| 美女黄网站人色视频免费国产| 色多多www视频在线观看免费| 亚洲邪恶天堂影院在线观看| 四虎永久在线精品免费影视| 亚洲欧洲精品成人久久曰影片 | 24小时免费直播在线观看| 亚洲熟妇少妇任你躁在线观看| 亚洲国产成人精品久久久国产成人一区二区三区综 | 亚洲风情亚Aⅴ在线发布| 久久亚洲国产成人精品无码区| 日韩在线不卡免费视频一区| 亚洲熟妇无码八V在线播放| 亚洲男人的天堂www| 国产乱码免费卡1卡二卡3卡| 免费夜色污私人影院网站电影|