将字段_id猫鼬更改为自动增量

时间:2018-10-15 11:16:59

标签: mongodb mongoose

我正在尝试更改猫鼬中的_id,但是我在范围上遇到问题,无法解决。

patrimonioSchema.pre('save', function(next) {
  if(this.isNew){
    var doc = this;

    counter.findByIdAndUpdate({_id: 'entityId'},{$inc: { seq: 1} },{new: true, upsert: true})
    .then(function(count) {
      console.log("...count: "+JSON.stringify(count));
      doc._id = count.seq; //dont change, variable local

      next();
    })
    .catch(function(error) {
        console.error("counter error-> : "+error);
        throw error;
    });

  }else{
    next()
  }
});

有人可以帮助我。

谢谢。

0 个答案:

没有答案
相关问题