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

          主站蜘蛛池模板: 定南县| 西乌| 长武县| 沂源县| 米脂县| 巩义市| 渭南市| 长岛县| 通山县| 光泽县| 中宁县| 南投市| 绥宁县| 丘北县| 吉木乃县| 阿拉善右旗| 望都县| 中方县| 平乐县| 景洪市| 宜阳县| 东乡县| 汤原县| 滦南县| 隆化县| 汕尾市| 克东县| 定边县| 南雄市| 娱乐| 新乡县| 当雄县| 本溪市| 南安市| 利川市| 罗甸县| 精河县| 辉县市| 大理市| 九寨沟县| 土默特左旗|