Skip to content

Commit 577d6eb

Browse files
authored
fix: only force server id generation if requested (#2653)
An inverted boolean expression accidentally always forced the driver to force the server to generate document ids. NODE-2923
1 parent 0cca729 commit 577d6eb

File tree

4 files changed

+37
-19
lines changed

4 files changed

+37
-19
lines changed

src/bulk/common.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1041,7 +1041,7 @@ export abstract class BulkOperationBase {
10411041
* ```
10421042
*/
10431043
insert(document: Document): BulkOperationBase {
1044-
if (document._id == null && shouldForceServerObjectId(this)) {
1044+
if (document._id == null && !shouldForceServerObjectId(this)) {
10451045
document._id = new ObjectId();
10461046
}
10471047

test/functional/abstract_cursor.test.js

+1-17
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,6 @@
11
'use strict';
22
const { expect } = require('chai');
3-
const { filterForCommands } = require('./shared');
4-
5-
function withClientV2(callback) {
6-
return function testFunction(done) {
7-
const client = this.configuration.newClient({ monitorCommands: true });
8-
client.connect(err => {
9-
if (err) return done(err);
10-
this.defer(() => client.close());
11-
12-
try {
13-
callback.call(this, client, done);
14-
} catch (err) {
15-
done(err);
16-
}
17-
});
18-
};
19-
}
3+
const { filterForCommands, withClientV2 } = require('./shared');
204

215
describe('AbstractCursor', function () {
226
before(

test/functional/bulk.test.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict';
2-
const { withClient, setupDatabase, ignoreNsNotFound } = require('./shared');
2+
const { withClient, withClientV2, setupDatabase, ignoreNsNotFound } = require('./shared');
33
const test = require('./shared').assert;
44
const { expect } = require('chai');
55
const { MongoError } = require('../../src/error');
@@ -1743,4 +1743,20 @@ describe('Bulk', function () {
17431743
.then(updatedAdam => expect(updatedAdam).property('age').to.equal(39));
17441744
});
17451745
});
1746+
1747+
it(
1748+
'should return correct ids for documents with generated ids',
1749+
withClientV2(function (client, done) {
1750+
const bulk = client.db().collection('coll').initializeUnorderedBulkOp();
1751+
for (let i = 0; i < 2; i++) bulk.insert({ x: 1 });
1752+
bulk.execute((err, result) => {
1753+
expect(err).to.not.exist;
1754+
expect(result).property('insertedIds').to.exist;
1755+
expect(Object.keys(result.insertedIds)).to.have.length(2);
1756+
expect(result.insertedIds[0]).to.exist;
1757+
expect(result.insertedIds[1]).to.exist;
1758+
done();
1759+
});
1760+
})
1761+
);
17461762
});

test/functional/shared.js

+18
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,23 @@ class APMEventCollector {
276276
}
277277
}
278278

279+
// simplified `withClient` helper that only uses callbacks
280+
function withClientV2(callback) {
281+
return function testFunction(done) {
282+
const client = this.configuration.newClient({ monitorCommands: true });
283+
client.connect(err => {
284+
if (err) return done(err);
285+
this.defer(() => client.close());
286+
287+
try {
288+
callback.call(this, client, done);
289+
} catch (err) {
290+
done(err);
291+
}
292+
});
293+
};
294+
}
295+
279296
module.exports = {
280297
assert,
281298
delay,
@@ -285,6 +302,7 @@ module.exports = {
285302
ignoreNsNotFound,
286303
setupDatabase,
287304
withClient,
305+
withClientV2,
288306
withMonitoredClient,
289307
withCursor,
290308
APMEventCollector

0 commit comments

Comments
 (0)