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

          主站蜘蛛池模板: 黑龙江省| 黎平县| 肥西县| 罗平县| 马山县| 绥化市| 宜良县| 印江| 瑞安市| 桑日县| 财经| 永年县| 闵行区| 克东县| 常山县| 荣成市| 诸暨市| 大连市| 凌源市| 新营市| 宜君县| 金阳县| 即墨市| 七台河市| 邯郸县| 富平县| 嘉善县| 宣武区| 蓝田县| 宜兴市| 澄江县| 内丘县| 星子县| 荔波县| 涿州市| 老河口市| 大连市| 彭州市| 富平县| 芷江| 兴海县|