mongodb move documents from one collection to another collection

前端 未结 15 1546
感情败类
感情败类 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 22:56

    you can use range query to get data from sourceCollection and keep the cursor data in variable and loop on it and insert to target collection:

     var doc = db.sourceCollection.find({
            "Timestamp":{
                  $gte:ISODate("2014-09-01T00:00:00Z"),
                  $lt:ISODate("2014-10-01T00:00:00Z")
            }
     });
    
     doc.forEach(function(doc){
        db.targetCollection.insert(doc);
     })
    

    Hope so it helps!!

提交回复
热议问题