paulwong

          How to delete large amount of data of a MongoDB collection “quickly”

          We have a db collection that is around 30 million documents, and I need to trim it down, to only keeping the documents created on the last month. 

          One approach would be use the remove command with a condition on the created_at field (the collection already have an index on this field):

          db.my_collection.remove({created_at: {$lte: new Date("11/01/2012")}});

          But this approach will be very slow, instead of that, a better way to do it is rename the current collection (for instance to “old_collection”) using renameCollection. Then performing a query-and-insert from the “old_collection” into “my_collection”:

          db.my_collection.renameCollection("old_collection");  
          db.createCollection("my_collection");
          db.my_collection.createIndex(...); // recreate the indexes for the collection
          // copy docs from old collection into the new collection
          db.old_collection.find(
          {created_at: {$gte: new Date("11/01/2012")}} ).sort({_id: -1}).forEach(
          function(row) { db.my_collection.insert(row); } ); // drop old collection db.old_collection.drop();

          This approach is typically faster than running a bunch of removes on your data

          posted on 2015-12-10 20:09 paulwong 閱讀(540) 評論(0)  編輯  收藏 所屬分類: MONGODB

          主站蜘蛛池模板: 勐海县| 井研县| 达尔| 杂多县| 永川市| 文水县| 嘉鱼县| 汉沽区| 象山县| 民权县| 永川市| 肥城市| 长沙市| 清原| 鹤庆县| 新和县| 镇雄县| 萨迦县| 阳朔县| 利川市| 衡南县| 嘉兴市| 繁峙县| 延安市| 龙南县| 土默特左旗| 太保市| 花垣县| 锡林郭勒盟| 静乐县| 封开县| 临夏市| 嘉荫县| 荔浦县| 和田县| 长子县| 宁武县| 清苑县| 阜城县| 静安区| 句容市|