Skip to content

Commit 36e92f1

Browse files
authored
fix(db_ops): call collection.find() with correct parameters (#1795)
Fixes NODE-1596
1 parent 759dd85 commit 36e92f1

File tree

1 file changed

+23
-24
lines changed

1 file changed

+23
-24
lines changed

lib/operations/db_ops.js

+23-24
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const toError = require('../utils').toError;
1515
const count = require('./collection_ops').count;
1616
const findOne = require('./collection_ops').findOne;
1717
const remove = require('./collection_ops').remove;
18-
const update = require('./collection_ops').update;
18+
const updateOne = require('./collection_ops').updateOne;
1919

2020
const debugFields = [
2121
'authSource',
@@ -90,28 +90,27 @@ function addUser(db, username, password, options, callback) {
9090
// We got an error (f.ex not authorized)
9191
if (err != null) return handleCallback(callback, err, null);
9292
// Check if the user exists and update i
93-
collection
94-
.find({ user: username }, { dbName: options['dbName'] }, finalOptions)
95-
.toArray(err => {
96-
// We got an error (f.ex not authorized)
97-
if (err != null) return handleCallback(callback, err, null);
98-
// Add command keys
99-
finalOptions.upsert = true;
100-
101-
// We have a user, let's update the password or upsert if not
102-
update(
103-
collection,
104-
{ user: username },
105-
{ $set: { user: username, pwd: userPassword } },
106-
finalOptions,
107-
err => {
108-
if (count === 0 && err)
109-
return handleCallback(callback, null, [{ user: username, pwd: userPassword }]);
110-
if (err) return handleCallback(callback, err, null);
111-
handleCallback(callback, null, [{ user: username, pwd: userPassword }]);
112-
}
113-
);
114-
});
93+
const findOptions = Object.assign({ projection: { dbName: 1 } }, finalOptions);
94+
collection.find({ user: username }, findOptions).toArray(err => {
95+
// We got an error (f.ex not authorized)
96+
if (err != null) return handleCallback(callback, err, null);
97+
// Add command keys
98+
finalOptions.upsert = true;
99+
100+
// We have a user, let's update the password or upsert if not
101+
updateOne(
102+
collection,
103+
{ user: username },
104+
{ $set: { user: username, pwd: userPassword } },
105+
finalOptions,
106+
err => {
107+
if (count === 0 && err)
108+
return handleCallback(callback, null, [{ user: username, pwd: userPassword }]);
109+
if (err) return handleCallback(callback, err, null);
110+
handleCallback(callback, null, [{ user: username, pwd: userPassword }]);
111+
}
112+
);
113+
});
115114
});
116115

117116
return;
@@ -592,7 +591,7 @@ function profilingInfo(db, options, callback) {
592591
try {
593592
db
594593
.collection('system.profile')
595-
.find({}, null, options)
594+
.find({}, options)
596595
.toArray(callback);
597596
} catch (err) {
598597
return callback(err, null);

0 commit comments

Comments
 (0)