mongodb move documents from one collection to another collection

前端 未结 15 1513
感情败类
感情败类 2020-11-30 22:10

How can documents be moved from one collection to another collection in MongoDB?? For example: I have lot of documents in

15条回答
  •  独厮守ぢ
    2020-11-30 23:01

    May be from the performance point of view it's better to remove a lot of documents using one command(especially if you have indexes for query part) rather than deleting them one-by-one.

    For example:

    db.source.find({$gte: start, $lt: end}).forEach(function(doc){
       db.target.insert(doc);
    });
    db.source.remove({$gte: start, $lt: end});
    

提交回复
热议问题