mongodb move documents from one collection to another collection

前端 未结 15 1550
感情败类
感情败类 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:09

    First option (Using mongo dump)

    1.Get a dump from collection

    mongodump -d db -c source_collection

    2.Restore from collection

    mongorestore -d db -c target_collection dir=dump/db_name/source_collection.bson

    Second Option

    Running aggregate

    db.getCollection('source_collection').aggregate([ { $match: {"emailAddress" : "apitester@mailinator.com"} }, { $out: "target_collection" } ])

    Third Option (Slowest)

    Running a through for loop

    db.getCollection('source_collection').find().forEach(function(docs){ db.getCollection('target_collection').insert(docs); }) print("Rolleback Completed!");

提交回复
热议问题