Skip to content

Node.js based forum software built for the modern web

fix decr/incrObjectField if key in array doesn't exist create it

Barış Soner Uşaklıbe5c23738835
diff --git a/src/database/mongo/hash.js b/src/database/mongo/hash.js
index 64815d5a8f..55ae9c7ea8 100644
--- a/src/database/mongo/hash.js
+++ b/src/database/mongo/hash.js
@@ -289,7 +289,10 @@ module.exports = function (db, module) {
if (Array.isArray(key)) {
var bulk = db.collection('objects').initializeUnorderedBulkOp();
- bulk.find({ _key: { $in: key } }).upsert().update({ $inc: data });
+ key.forEach(function (key) {
+ bulk.find({ _key: key }).upsert().update({ $inc: data });
+ });
+
async.waterfall([
function (next) {
bulk.execute(function (err) {
diff --git a/test/database/hash.js b/test/database/hash.js
index 88a61812a9..b053b111ed 100644
--- a/test/database/hash.js
+++ b/test/database/hash.js
@@ -408,10 +408,11 @@ describe('Hash methods', function () {
});
it('should decrement multiple objects field by 1 and return an array of new values', function (done) {
- db.decrObjectField(['testObject13', 'testObject14'], 'age', function (err, data) {
+ db.decrObjectField(['testObject13', 'testObject14', 'decrTestObject'], 'age', function (err, data) {
assert.ifError(err);
assert.equal(data[0], 97);
assert.equal(data[1], -1);
+ assert.equal(data[2], -1);
done();
});
});