Node.js 猫鼬,复制一份文件

Node.js 猫鼬,复制一份文件,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,正在尝试复制文档。首先我找到了它。然后删除_id,然后插入它。但是计算仍然存在。所以我抛出了一个复制错误。我做错了什么 mongoose.model('calculations').findOne({calcId:req.params.calcId}, function(){ if(err) handleErr(err, res, 'Something went wrong when trying to find calculation by id'); delete

正在尝试复制文档。首先我找到了它。然后删除_id,然后插入它。但是计算仍然存在。所以我抛出了一个复制错误。我做错了什么

mongoose.model('calculations').findOne({calcId:req.params.calcId}, function(){
      if(err) handleErr(err, res, 'Something went wrong when trying to find calculation by id');

      delete calculation._id;
      console.log(calculation); //The _.id is still there
      mongoose.model('calculations').create(calculation, function(err, stat){
        if(err) handleErr(err, res, 'Something went wrong when trying to copy a calculation');
        res.send(200);
      })
    });

从findOne返回的对象不是普通对象,而是Mongoose文档。您应该使用
{lean:true}
选项或
.toObject()
方法将其转换为普通JavaScript对象

mongoose.model('calculations').findOne({calcId:req.params.calcId}, function(err,calculation){
  if(err) handleErr(err, res, 'Something went wrong when trying to find calculation by id');

  var plainCalculation = calculation.toObject();


  delete plainCalculation._id;
  console.log(plainCalculation); //no _id here
});