mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 16:46:12 +01:00
fix: retry incrObjtFieldBy
This commit is contained in:
@@ -239,17 +239,26 @@ module.exports = function (module) {
|
|||||||
const result = await module.getObjectsFields(key, [field]);
|
const result = await module.getObjectsFields(key, [field]);
|
||||||
return result.map(data => data && data[field]);
|
return result.map(data => data && data[field]);
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
const result = await module.client.collection('objects').findOneAndUpdate({
|
const result = await module.client.collection('objects').findOneAndUpdate({
|
||||||
_key: key,
|
_key: key,
|
||||||
}, {
|
}, {
|
||||||
$inc: increment,
|
$inc: increment,
|
||||||
}, {
|
}, {
|
||||||
returnDocument: 'after',
|
returnDocument: 'after',
|
||||||
upsert: true,
|
upsert: true,
|
||||||
});
|
});
|
||||||
|
cache.del(key);
|
||||||
cache.del(key);
|
return result && result.value ? result.value[field] : null;
|
||||||
return result && result.value ? result.value[field] : null;
|
} catch (err) {
|
||||||
|
// if there is duplicate key error retry the upsert
|
||||||
|
// https://github.com/NodeBB/NodeBB/issues/4467
|
||||||
|
// https://jira.mongodb.org/browse/SERVER-14322
|
||||||
|
// https://docs.mongodb.org/manual/reference/command/findAndModify/#upsert-and-unique-index
|
||||||
|
if (err && err.message.startsWith('E11000 duplicate key error')) {
|
||||||
|
return await module.incrObjectFieldBy(key, field, value);
|
||||||
|
}
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user