Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 4 additions & 1 deletion lib/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,10 @@ Collection.prototype.updateOne = function(filter, update, options, callback) {
if (typeof options === 'function') (callback = options), (options = {});
options = options || {};

const err = checkForAtomicOperators(update);
const err = Array.isArray(update)
? update.forEach(checkForAtomicOperators)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

from what I can tell, this is not going to return an err, as Array.prototype.forEach returns undefined.

Is the goal here to get the first checkForAtomicOperators error on the pipeline?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Yep, I believe that we want the first checkForAtomicOperators error in the pipeline.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

In that case you could build this up imperatively:

let err;
if (Array.isArray(update)) {
  for (let i = 0; !err && i < update.length; i++) {
    err = checkForAtomicOperators(update);
  }
} else {
  err = checkForAtomicOperators(update);
}

: checkForAtomicOperators(update);

if (err) {
if (typeof callback === 'function') return callback(err);
return this.s.promiseLibrary.reject(err);
Expand Down
32 changes: 32 additions & 0 deletions test/functional/collection_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1898,4 +1898,36 @@ describe('Collection', function() {
}
});
});

it('should correctly update with array of docs', {
metadata: {
requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] }
},

// The actual test we wish to run
test: function(done) {
const configuration = this.configuration;
const client = configuration.newClient(configuration.writeConcernMax(), {
poolSize: 1
});

client.connect((err, client) => {
const db = client.db(configuration.db);

db.createCollection('test_should_correctly_do_update_with_docs_array', (err, collection) => {
collection.updateOne(
{},
[{ $set: { a: 1 } }, { $set: { b: 1 } }, { $set: { d: 1 } }],
configuration.writeConcernMax(),
(err, r) => {
expect(err).to.equal(null);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We can use expect(err).to.not.exist.

expect(r.result.n).to.equal(0);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why do we expect this to be 0?


client.close(done);
}
);
});
});
}
});
});