0
Model.findById(req.body.myid, function (err, results) {
      var doc = new Model(results);
      doc._id = mongoose.Types.ObjectId();
      doc.serial = req.body.serial;
      doc.remarks = req.body.remarks;
      doc.save(function(err) {
        if(err){
          res.json({ success: false });
        }else {
          res.json({ success: true });
        }
      });
    });

How can i create copy of a document and save using mongoosejs ??

Sri Rag
  • 13
  • 1
  • 5
  • 1
    why this doesn't work? what is the error? try maybe to do `results.toObject()` – TomG Sep 19 '16 at 15:22
  • 1
    Please look at this question: https://stackoverflow.com/questions/18324843/easiest-way-to-copy-clone-a-mongoose-document-instance – Pter Sep 19 '16 at 15:23
  • `code` (node:8260) DeprecationWarning: Mongoose: mpromise (mongoose's default promise library) is deprecated, plug in your own promise library instead: http://mongoosejs.com/docs/promises.html { Error at model.wrappedPointCut [as save] message: 'No matching document found for id, name: 'VersionError' } `code` – Sri Rag Sep 19 '16 at 15:39
  • `code` doc.isNew = true;`code` – Sri Rag Sep 19 '16 at 15:52
  • Thankyou Tom & Pter.... worked! – Sri Rag Sep 19 '16 at 15:52

1 Answers1

1

Also need to set isNew to true

doc._id = mongoose.Types.ObjectId();
doc.serial = req.body.serial;
doc.remarks = req.body.remarks;
doc.isNew = true;
Rifky Niyas
  • 1,737
  • 10
  • 25