Skip to content

Commit

Permalink
fix(db_ops): call collection.find() with correct parameters (mongodb#…
Browse files Browse the repository at this point in the history
…1795)

Fixes NODE-1596
  • Loading branch information
kvwalker authored and kiku-jw committed Feb 11, 2019
1 parent 871a2e8 commit 392f357
Showing 1 changed file with 23 additions and 24 deletions.
47 changes: 23 additions & 24 deletions lib/operations/db_ops.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 392f357

Please sign in to comment.