mongo copy from one collection to another (on the same db)

后端 未结 7 607
暖寄归人
暖寄归人 2020-12-24 08:02

I have got mongo db called test and in this db two collections collection1 and collection1_backup. How to replace content of col

7条回答
  •  被撕碎了的回忆
    2020-12-24 08:40

    Better way would be to use .toArray()

     db.collection1.drop(); // Drop entire other collection
    
     // creates an array which can be accessed from "data"
     db.collection1_backup.find().toArray(function(err, data) {
    
          // creates a collection and inserting the array at once
          db.collection1.insert(data);
     });
    

提交回复
热议问题