mongoose-id-autoinc
mongoose-id-autoinc copied to clipboard
不兼容较新 mongoose 4.0.4
因为用了 koa 框架,自然用到了 yield 特性,所以 mongoose 升级到了 4.0.4 ,同时 mongodb 也迁移到了 较新的 3.0.3,但是 autoinc 却失效了,老的线上数据再加新数据,这个字段基本宣告失效了,业务逻辑几乎不能走动了。
期待作者能把这个小插件维护下去啊,帮助到很多!
感谢!!!
这个找到了原因和解决方案:
Counter.collection.findAndModify({
model: options.model,
field: options.field
}, [], {
$inc: {
seq: options.step
}
}, {
'new': true,
upsert: true
}, function (err, res) {
这里 res 返回的值是嵌套的,类似:
{
value: {
_id: 53ecbd09008b09bb2c17d78a,
model: 'channelunikey',
field: 'channelid',
seq: 46,
__v: 0
},
lastErrorObject: { updatedExisting: true, n: 1 },
ok: 1
}
因此把
if (!err) doc[options.field] = res.seq - options.step
替换成:
if (!err) {
var seq
if (res.seq) {
seq = res.seq
}
else if (res.value && res.value.seq) {
seq = res.value.seq
}
doc[options.field] = seq - options.step
}
即可,先这样 fix 掉了,这个 issue 可以关掉了