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 ??

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

1 Answer 1

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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.