diff --git a/lib/operations/db_ops.js b/lib/operations/db_ops.js index 9e35f7d344..af0210a97b 100644 --- a/lib/operations/db_ops.js +++ b/lib/operations/db_ops.js @@ -15,7 +15,7 @@ const toError = require('../utils').toError; const count = require('./collection_ops').count; const findOne = require('./collection_ops').findOne; const remove = require('./collection_ops').remove; -const update = require('./collection_ops').update; +const updateOne = require('./collection_ops').updateOne; const debugFields = [ 'authSource', @@ -90,28 +90,27 @@ function addUser(db, username, password, options, callback) { // We got an error (f.ex not authorized) if (err != null) return handleCallback(callback, err, null); // Check if the user exists and update i - collection - .find({ user: username }, { dbName: options['dbName'] }, finalOptions) - .toArray(err => { - // We got an error (f.ex not authorized) - if (err != null) return handleCallback(callback, err, null); - // Add command keys - finalOptions.upsert = true; - - // We have a user, let's update the password or upsert if not - update( - collection, - { user: username }, - { $set: { user: username, pwd: userPassword } }, - finalOptions, - err => { - if (count === 0 && err) - return handleCallback(callback, null, [{ user: username, pwd: userPassword }]); - if (err) return handleCallback(callback, err, null); - handleCallback(callback, null, [{ user: username, pwd: userPassword }]); - } - ); - }); + const findOptions = Object.assign({ projection: { dbName: 1 } }, finalOptions); + collection.find({ user: username }, findOptions).toArray(err => { + // We got an error (f.ex not authorized) + if (err != null) return handleCallback(callback, err, null); + // Add command keys + finalOptions.upsert = true; + + // We have a user, let's update the password or upsert if not + updateOne( + collection, + { user: username }, + { $set: { user: username, pwd: userPassword } }, + finalOptions, + err => { + if (count === 0 && err) + return handleCallback(callback, null, [{ user: username, pwd: userPassword }]); + if (err) return handleCallback(callback, err, null); + handleCallback(callback, null, [{ user: username, pwd: userPassword }]); + } + ); + }); }); return; @@ -592,7 +591,7 @@ function profilingInfo(db, options, callback) { try { db .collection('system.profile') - .find({}, null, options) + .find({}, options) .toArray(callback); } catch (err) { return callback(err, null);