如何使用Mongoose JS复制文档

时间:2016-09-19 15:18:16

标签: node.js mongodb mongoose

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 });
        }
      });
    });
  

如何创建文档副本并使用mongoosejs保存?

1 个答案:

答案 0 :(得分:0)

还需要将isNew设置为true

  doc._id = mongoose.Types.ObjectId();
  doc.serial = req.body.serial;
  doc.remarks = req.body.remarks;
  doc.isNew = true;