diff --git a/src/database/mongo/hash.js b/src/database/mongo/hash.js index 791cbc12cc..d887143d89 100644 --- a/src/database/mongo/hash.js +++ b/src/database/mongo/hash.js @@ -7,6 +7,9 @@ module.exports = function(db, module) { module.setObject = function(key, data, callback) { callback = callback || helpers.noop; + if (!key) { + return callback(); + } data._key = key; db.collection('objects').update({_key:key}, {$set:data}, {upsert:true, w: 1}, function(err) { callback(err); @@ -21,10 +24,16 @@ module.exports = function(db, module) { }; module.getObject = function(key, callback) { - db.collection('objects').findOne({_key:key}, {_id:0, _key:0}, callback); + if (!key) { + return callback(); + } + db.collection('objects').findOne({_key: key}, {_id:0, _key:0}, callback); }; module.getObjects = function(keys, callback) { + if (!Array.isArray(keys) || !keys.length) { + return callback(); + } db.collection('objects').find({_key: {$in: keys}}, {_id: 0}).toArray(function(err, data) { if(err) { return callback(err); @@ -42,19 +51,52 @@ module.exports = function(db, module) { }; module.getObjectField = function(key, field, callback) { + if (!key) { + return callback(); + } field = helpers.fieldToString(field); - module.getObjectFields(key, [field], function(err, data) { - callback(err, data ? data[field] : null); + var _fields = { + _id: 0 + }; + _fields[field] = 1; + db.collection('objects').findOne({_key: key}, _fields, function(err, item) { + if (err || !item) { + return callback(err, null); + } + + callback(null, item[field] || null); }); }; module.getObjectFields = function(key, fields, callback) { - module.getObjectsFields([key], fields, function(err, items) { - callback(err, items ? items[0] : null); + if (!key) { + return callback(); + } + var _fields = { + _id: 0 + }; + + for(var i=0; i