paulwong

          My Links

          Blog Stats

          常用鏈接

          留言簿(67)

          隨筆分類(1392)

          隨筆檔案(1150)

          文章分類(7)

          文章檔案(10)

          相冊

          收藏夾(2)

          AI

          Develop

          E-BOOK

          Other

          養生

          微服務

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          60天內閱讀排行

          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

          主站蜘蛛池模板: 依安县| 府谷县| 宁武县| 东源县| 开原市| 平谷区| 陆良县| 哈巴河县| 娄烦县| 洪泽县| 黎城县| 西城区| 枞阳县| 安福县| 峡江县| 哈尔滨市| 石门县| 衡山县| 遵义县| 山丹县| 沂南县| 北安市| 天气| 冷水江市| 德兴市| 吐鲁番市| 岳池县| 南安市| 大方县| 静安区| 钦州市| 区。| 大庆市| 益阳市| 伽师县| 绍兴县| 巴南区| 洛川县| 伊吾县| 洞头县| 长寿区|