Easiest way to copy/clone a mongoose document instance?

醉酒当歌 提交于 2019-11-27 03:19:24

问题


My approach would be to get the document instance, and create a new one from the instance fields. I am sure there is a better way to do it.


回答1:


Can you clarify what you mean by "copy/clone"? Are you going trying to create a duplicate document in the database? Or are you just trying to have two vars in your program that have duplicate data?

If you just do:

Model.findById(yourid).exec(
    function(err, doc) { 
        var x = doc; 
        Model.findById(yourid).exec(
            function(err, doc2) {
                var y = doc2;
                // right now, x.name and y.name are the same
                x.name = "name_x";
                y.name = "name_y";
                console.log(x.name); // prints "name_x"
                console.log(y.name); // prints "name_y"
            }); 
    });

In this case, x and y will be two "copies" of the same document within your program.

Alternatively, if you wanted to insert a new copy of the doc into the database (though with a different _id I assume), that would look like this:

Model.findById(yourid).exec(
    function(err, doc) {
        var d1 = doc;
        d1._id = /* set a new _id here */;
        d1.save(callback);
    }
);

Or if you're doing this from the outset, aka you created some document d1, you can just call save twice without setting the _id:

var d1 = new Model({ name: "John Doe", age: 54 });
d1.save(callback);
d1.save(callback);

There will now be two documents with differing _id's and all other fields identical in your database.

Does this clarify things a bit?




回答2:


The following code to clone documents in Amelia's response above does not work:

Model.findById(yourid).exec(
    function(err, doc) {
        var d1 = doc;
        d1._id = /* set a new _id here */;
        d1.save(callback);
    }
);

You also need to reset d1.isNew = true; as in:

Model.findById(yourid).exec(
    function(err, doc) {
        doc._id = mongoose.Types.ObjectId();
        doc.isNew = true; //<--------------------IMPORTANT
        doc.save(callback);
    }
);



回答3:


The following code to clone documents:

Model.findById(yourid).exec(
        function(err, doc) {
            var newdoc = new Model(doc);
            newdoc ._id = mongoose.Types.ObjectId();
            newdoc .save(callback);
        }
    );



回答4:


For simply clone use this :

Context.findOne({
    _id: context._id
})
    .then(function(c) {
        c._id = undefined;
        c.name = context.name;
        c.address = context.address;
        c.created = Date.now();
        return Context.create(c.toObject());
    }).then(function(c) {
        return res.json({
            success: true,
            context: context
        });
    }).catch(function(err) {
        next(err, req, res);
    });


来源:https://stackoverflow.com/questions/18324843/easiest-way-to-copy-clone-a-mongoose-document-instance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!