final fix for #1884

fix mongo functions so they return correct data if duplicate keys are
passed. getObjects(['post:1', 'post:1'], callback) was only returning
one object.
v1.18.x
barisusakli 11 years ago
parent 3557aa2f9a
commit 8dce99250b

@ -20,17 +20,16 @@ module.exports = function(db, module) {
};
module.getObjects = function(keys, callback) {
db.collection('objects').find({_key:{$in:keys}}, {_id:0}).toArray(function(err, data) {
db.collection('objects').find({_key: {$in: keys}}, {_id: 0}).toArray(function(err, data) {
if(err) {
return callback(err);
}
var map = helpers.toMap(data);
var returnData = [];
for(var i=0; i<keys.length; ++i) {
returnData.push(helpers.findItem(data, keys[i]));
returnData.push(map[keys[i]]);
}
callback(null, returnData);
@ -70,12 +69,13 @@ module.exports = function(db, module) {
items = [];
}
var map = helpers.toMap(items);
var returnData = [],
index = 0,
item;
for (var i=0; i<keys.length; ++i) {
var item = helpers.findItem(items, keys[i]) || {};
var item = map[keys[i]] || {};
for (var k=0; k<fields.length; ++k) {
if (item[fields[k]] === null || item[fields[k]] === undefined) {

@ -2,23 +2,13 @@
var helpers = {};
helpers.findItem = function(data, key) {
if(!data) {
return null;
helpers.toMap = function(data) {
var map = {};
for (var i = 0; i<data.length; ++i) {
map[data[i]._key] = data[i];
}
for(var i=0; i<data.length; ++i) {
if(data[i]._key === key) {
var item = data.splice(i, 1);
if(item && item.length) {
return item[0];
} else {
return null;
}
}
}
return null;
};
return map;
}
helpers.fieldToString = function(field) {
if(field === null || field === undefined) {

@ -155,11 +155,12 @@ module.exports = function(db, module) {
return callback(err);
}
var returnData = [],
var map = helpers.toMap(result),
returnData = [],
item;
for(var i=0; i<keys.length; ++i) {
item = helpers.findItem(result, keys[i]);
item = map[keys[i]];
returnData.push(item ? item.score : null);
}

Loading…
Cancel
Save