Clone a collection in MongoDB

后端 未结 8 581
有刺的猬
有刺的猬 2020-12-02 10:19

I want to clone a MongoDB collection and save it on the same server with a different name. So for example right now I have the following collections: demo1.categories, demo1

8条回答
  •  盖世英雄少女心
    2020-12-02 10:48

    The most simple & efficient way is by using copyTo(), so you can use:

    db.source.copyTo("target"); 
    

    & if "target" doesn't exist, it will be created

    -- Update --

    According to CopyTo Documentation, Because copyTo() uses eval internally, the copy operations will block all other operations on the mongod instance. So it shouldn't be used on production environment.

    -- Update --

    Because CopyTo() uses eval() internally & eval() is deprecated since version 3.0, so CopyTo() is also deprecated since version 3.0.

提交回复
热议问题