Skip to content

Node.js based forum software built for the modern web

retry failed upserts closes #4467

barisusaklia9cc12c6673f
diff --git a/src/database/mongo/sorted.js b/src/database/mongo/sorted.js
index 0baa08b2f6..d8a9205ce0 100644
--- a/src/database/mongo/sorted.js
+++ b/src/database/mongo/sorted.js
@@ -524,6 +524,13 @@ module.exports = function(db, module) {
data.score = parseInt(increment, 10);
db.collection('objects').findAndModify({_key: key, value: value}, {}, {$inc: data}, {new: true, upsert: true}, function(err, result) {
+ // 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 module.sortedSetIncrBy(key, increment, value, callback);
+ }
callback(err, result && result.value ? result.value.score : null);
});
};