Duplicate a mongodb collection

前端 未结 2 1524
死守一世寂寞
死守一世寂寞 2021-01-14 13:07

What is a proper way to duplicate a collection in Mongodb on the same server using C#? MongoVUE has an option \'Duplicate collection\', is there something similar for C#?

2条回答
  •  遥遥无期
    2021-01-14 13:26

    There isn't a built-in way to copy collections with the C# driver, but you can still do it pretty simply as:

    var source = db.GetCollection("test");
    var dest = db.GetCollection("testcopy");
    dest.InsertBatch(source.FindAll());
    

    Note, however, that this won't copy any indexes from the source collection. The shell's copyTo method has the same limitation so it's likely implemented similarly.

提交回复
热议问题