Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(db_ops): call collection.find() with correct parameters #1795

Merged
merged 4 commits into from
Aug 8, 2018
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 }]);
}
);
});
finalOptions.projection = { dbName: 1 };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

finalOptions is subsequently used for the below updateOne call, so you have inadvertently added a projection to that command.

collection.find({ user: username }, 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
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