diff --git a/CHANGES_NEXT_RELEASE b/CHANGES_NEXT_RELEASE index 0b5f7fa67..061703f4c 100644 --- a/CHANGES_NEXT_RELEASE +++ b/CHANGES_NEXT_RELEASE @@ -1 +1,2 @@ - Set Nodejs 12 as minimum version in packages.json (effectively removing Nodev10 from supported versions) +- Update MongoDB to use lean() for faster retrieval diff --git a/lib/services/devices/deviceRegistryMongoDB.js b/lib/services/devices/deviceRegistryMongoDB.js index 656be6c0d..1090eb615 100644 --- a/lib/services/devices/deviceRegistryMongoDB.js +++ b/lib/services/devices/deviceRegistryMongoDB.js @@ -188,26 +188,11 @@ function listDevices(type, service, subservice, limit, offset, callback) { }); } -/** - * Internal function used to find a device in the DB. - * - * @param {String} id ID of the Device to find. - * @param {String} service Service the device belongs to (optional). - * @param {String} subservice Division inside the service (optional). - */ -function getDeviceById(id, service, subservice, callback) { - const queryParams = { - id, - service, - subservice - }; - context = fillService(context, queryParams); - logger.debug(context, 'Looking for device with id [%s].', id); - +function findOneInMongoDB(queryParams, id, callback) { const query = Device.model.findOne(queryParams); query.select({ __v: 0 }); - query.exec(function handleGet(error, data) { + query.lean().exec(function handleGet(error, data) { if (error) { logger.debug(context, 'Internal MongoDB Error getting device: %s', error); @@ -224,6 +209,24 @@ function getDeviceById(id, service, subservice, callback) { }); } +/** + * Internal function used to find a device in the DB. + * + * @param {String} id ID of the Device to find. + * @param {String} service Service the device belongs to (optional). + * @param {String} subservice Division inside the service (optional). + */ +function getDeviceById(id, service, subservice, callback) { + const queryParams = { + id, + service, + subservice + }; + context = fillService(context, queryParams); + logger.debug(context, 'Looking for device with id [%s].', id); + findOneInMongoDB(queryParams, id, callback); +} + /** * Retrieves a device using it ID, converting it to a plain Object before calling the callback. * @@ -236,7 +239,7 @@ function getDevice(id, service, subservice, callback) { if (error) { callback(error); } else { - callback(null, data.toObject()); + callback(null, data); } }); } @@ -253,13 +256,13 @@ function getByName(name, service, servicepath, callback) { query.select({ __v: 0 }); - query.exec(function handleGet(error, data) { + query.lean().exec(function handleGet(error, data) { if (error) { logger.debug(context, 'Internal MongoDB Error getting device: %s', error); callback(new errors.InternalDbError(error)); } else if (data) { - callback(null, data.toObject()); + callback(null, data); } else { logger.debug(context, 'Device [%s] not found.', name); @@ -293,7 +296,9 @@ function update(device, callback) { data.explicitAttrs = device.explicitAttrs; data.ngsiVersion = device.ngsiVersion; - data.save(saveDeviceHandler(callback)); + /* eslint-disable-next-line new-cap */ + const deviceObj = new Device.model(data); + deviceObj.save(saveDeviceHandler(callback)); } }); } diff --git a/lib/services/groups/groupRegistryMongoDB.js b/lib/services/groups/groupRegistryMongoDB.js index 76edcfe82..cb6bb2efc 100644 --- a/lib/services/groups/groupRegistryMongoDB.js +++ b/lib/services/groups/groupRegistryMongoDB.js @@ -36,6 +36,30 @@ let context = { op: 'IoTAgentNGSI.MongoDBGroupRegister' }; +const attributeList = [ + 'url', + 'resource', + 'apikey', + 'type', + 'service', + 'subservice', + 'description', + 'trust', + 'cbHost', + 'timezone', + 'timestamp', + 'commands', + 'lazy', + 'attributes', + 'staticAttributes', + 'internalAttributes', + 'autoprovision', + 'explicitAttrs', + 'expressionLanguage', + 'defaultEntityNameConjunction', + 'ngsiVersion' +]; + /** * Generates a handler for the save device group operations. The handler will take the customary error and the saved * device group as the parameters (and pass the serialized DAO as the callback value). @@ -58,33 +82,10 @@ function saveGroupHandler(groupDAO, callback) { function createGroup(group, callback) { /* eslint-disable-next-line new-cap */ const groupObj = new Group.model(); - const attributeList = [ - 'url', - 'resource', - 'apikey', - 'type', - 'service', - 'subservice', - 'description', - 'trust', - 'cbHost', - 'timezone', - 'timestamp', - 'commands', - 'lazy', - 'attributes', - 'staticAttributes', - 'internalAttributes', - 'autoprovision', - 'explicitAttrs', - 'expressionLanguage', - 'defaultEntityNameConjunction', - 'ngsiVersion' - ]; - - for (let i = 0; i < attributeList.length; i++) { - groupObj[attributeList[i]] = group[attributeList[i]]; - } + + attributeList.forEach((key) => { + groupObj[key] = group[key]; + }); logger.debug( context, @@ -163,7 +164,7 @@ function getById(id, callback) { const query = Group.model.findOne({ _id: id }); query.select({ __v: 0 }); - query.exec(function handleGet(error, data) { + query.lean().exec(function handleGet(error, data) { if (error) { logger.debug(context, 'Internal MongoDB Error getting group: %s', error); @@ -210,6 +211,24 @@ function find(service, subservice, callback) { }); } +function findOneInMongoDB(queryObj, fields, callback) { + const query = Group.model.findOne(queryObj); + query.select({ __v: 0 }); + query.lean().exec(function handleGet(error, data) { + if (error) { + logger.debug(context, 'Internal MongoDB Error getting group: %s', error); + callback(new errors.InternalDbError(error)); + } else if (data) { + context = fillService(context, data); + logger.debug(context, 'Device group data found: %j', data); + callback(null, data); + } else { + logger.debug(context, 'Device group for fields [%j] not found: [%j]', fields, queryObj); + callback(new errors.DeviceGroupNotFound(fields, queryObj)); + } + }); +} + function findBy(fields) { return function () { const queryObj = {}; @@ -228,24 +247,7 @@ function findBy(fields) { context = fillService(context, { service: 'n/a', subservice: 'n/a' }); logger.debug(context, 'Looking for group params %j with queryObj %j', fields, queryObj); - const query = Group.model.findOne(queryObj); - - query.select({ __v: 0 }); - - query.exec(function handleGet(error, data) { - if (error) { - logger.debug(context, 'Internal MongoDB Error getting group: %s', error); - callback(new errors.InternalDbError(error)); - } else if (data) { - context = fillService(context, data); - logger.debug(context, 'Device group data found: %j', data.toObject()); - callback(null, data.toObject()); - } else { - logger.debug(context, 'Device group for fields [%j] not found: [%j]', fields, queryObj); - - callback(new errors.DeviceGroupNotFound(fields, queryObj)); - } - }); + findOneInMongoDB(queryObj, fields, callback); }; } @@ -255,36 +257,15 @@ function update(id, body, callback) { if (error) { callback(error); } else { - const attributes = [ - 'url', - 'apikey', - 'type', - 'service', - 'subservice', - 'description', - 'trust', - 'cbHost', - 'timezone', - 'timestamp', - 'commands', - 'lazy', - 'attributes', - 'staticAttributes', - 'internalAttributes', - 'explicitAttrs', - 'expressionLanguage', - 'defaultEntityNameConjunction', - 'ngsiVersion' - ]; - - for (let i = 0; i < attributes.length; i++) { - if (body[attributes[i]] !== undefined) { - group[attributes[i]] = body[attributes[i]]; + attributeList.forEach((key) => { + if (body[key] !== undefined) { + group[key] = body[key]; } - } - - group.isNew = false; - group.save(saveGroupHandler(group, callback)); + }); + /* eslint-disable-next-line new-cap */ + const groupObj = new Group.model(group); + groupObj.isNew = false; + groupObj.save(saveGroupHandler(groupObj, callback)); } }); } @@ -303,7 +284,6 @@ function remove(id, callback) { callback(new errors.InternalDbError(error)); } else { logger.debug(context, 'Device [%s] successfully removed.', id); - callback(null, deviceGroup); } });