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) { 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) { if(err) {
return callback(err); return callback(err);
} }
var map = helpers.toMap(data);
var returnData = []; var returnData = [];
for(var i=0; i<keys.length; ++i) { for(var i=0; i<keys.length; ++i) {
returnData.push(helpers.findItem(data, keys[i])); returnData.push(map[keys[i]]);
} }
callback(null, returnData); callback(null, returnData);
@ -70,12 +69,13 @@ module.exports = function(db, module) {
items = []; items = [];
} }
var map = helpers.toMap(items);
var returnData = [], var returnData = [],
index = 0, index = 0,
item; item;
for (var i=0; i<keys.length; ++i) { 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) { for (var k=0; k<fields.length; ++k) {
if (item[fields[k]] === null || item[fields[k]] === undefined) { if (item[fields[k]] === null || item[fields[k]] === undefined) {

@ -2,23 +2,13 @@
var helpers = {}; var helpers = {};
helpers.findItem = function(data, key) { helpers.toMap = function(data) {
if(!data) { var map = {};
return null; for (var i = 0; i<data.length; ++i) {
map[data[i]._key] = data[i];
} }
return map;
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;
};
helpers.fieldToString = function(field) { helpers.fieldToString = function(field) {
if(field === null || field === undefined) { if(field === null || field === undefined) {

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

Loading…
Cancel
Save