猫鼬(mongo),复制文档

试图复制文档。首先,我找到了。然后删除_id。然后将其插入。但是calculation._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);
      })
    });
安德烈(Andrei Beziazychnyi)

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章