Skip to content

Commit 3217d9f

Browse files
committed
Merge branch '2.0' into 3.2-features
2 parents 78be162 + aabbfae commit 3217d9f

File tree

4 files changed

+137
-10
lines changed

4 files changed

+137
-10
lines changed

lib/bulk/ordered.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,10 @@ var define = OrderedBulkOperation.define = new Define('OrderedBulkOperation', Or
296296
OrderedBulkOperation.prototype.raw = function(op) {
297297
var key = Object.keys(op)[0];
298298

299+
// Set up the force server object id
300+
var forceServerObjectId = typeof this.s.options.forceServerObjectId == 'boolean'
301+
? this.s.options.forceServerObjectId : this.s.collection.s.db.options.forceServerObjectId;
302+
299303
// Update operations
300304
if((op.updateOne && op.updateOne.q)
301305
|| (op.updateMany && op.updateMany.q)
@@ -327,16 +331,16 @@ OrderedBulkOperation.prototype.raw = function(op) {
327331

328332
// Insert operations
329333
if(op.insertOne && op.insertOne.document == null) {
330-
if(op.insertOne._id == null) op.insertOne._id = new ObjectID();
334+
if(forceServerObjectId !== true && op.insertOne._id == null) op.insertOne._id = new ObjectID();
331335
return addToOperationsList(this, common.INSERT, op.insertOne);
332336
} else if(op.insertOne && op.insertOne.document) {
333-
if(op.insertOne.document._id == null) op.insertOne.document._id = new ObjectID();
337+
if(forceServerObjectId !== true && op.insertOne.document._id == null) op.insertOne.document._id = new ObjectID();
334338
return addToOperationsList(this, common.INSERT, op.insertOne.document);
335339
}
336340

337341
if(op.insertMany) {
338342
for(var i = 0; i < op.insertMany.length; i++) {
339-
if(op.insertMany[i]._id == null) op.insertMany[i]._id = new ObjectID();
343+
if(forceServerObjectId !== true && op.insertMany[i]._id == null) op.insertMany[i]._id = new ObjectID();
340344
addToOperationsList(this, common.INSERT, op.insertMany[i]);
341345
}
342346

@@ -355,7 +359,7 @@ OrderedBulkOperation.prototype.raw = function(op) {
355359
* @return {OrderedBulkOperation}
356360
*/
357361
OrderedBulkOperation.prototype.insert = function(document) {
358-
if(document._id == null) document._id = new ObjectID();
362+
if(this.s.collection.s.db.options.forceServerObjectId !== true && document._id == null) document._id = new ObjectID();
359363
return addToOperationsList(this, common.INSERT, document);
360364
}
361365

lib/bulk/unordered.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ var define = UnorderedBulkOperation.define = new Define('UnorderedBulkOperation'
310310
* @return {UnorderedBulkOperation}
311311
*/
312312
UnorderedBulkOperation.prototype.insert = function(document) {
313-
if(document._id == null) document._id = new ObjectID();
313+
if(this.s.collection.s.db.options.forceServerObjectId !== true && document._id == null) document._id = new ObjectID();
314314
return addToOperationsList(this, common.INSERT, document);
315315
}
316316

@@ -345,6 +345,10 @@ Object.defineProperty(UnorderedBulkOperation.prototype, 'length', {
345345
UnorderedBulkOperation.prototype.raw = function(op) {
346346
var key = Object.keys(op)[0];
347347

348+
// Set up the force server object id
349+
var forceServerObjectId = typeof this.s.options.forceServerObjectId == 'boolean'
350+
? this.s.options.forceServerObjectId : this.s.collection.s.db.options.forceServerObjectId;
351+
348352
// Update operations
349353
if((op.updateOne && op.updateOne.q)
350354
|| (op.updateMany && op.updateMany.q)
@@ -376,15 +380,16 @@ UnorderedBulkOperation.prototype.raw = function(op) {
376380

377381
// Insert operations
378382
if(op.insertOne && op.insertOne.document == null) {
379-
if(op.insertOne._id == null) op.insertOne._id = new ObjectID();
383+
if(forceServerObjectId !== true && op.insertOne._id == null) op.insertOne._id = new ObjectID();
380384
return addToOperationsList(this, common.INSERT, op.insertOne);
381385
} else if(op.insertOne && op.insertOne.document) {
382-
if(op.insertOne.document._id == null) op.insertOne.document._id = new ObjectID();
386+
if(forceServerObjectId !== true && op.insertOne.document._id == null) op.insertOne.document._id = new ObjectID();
383387
return addToOperationsList(this, common.INSERT, op.insertOne.document);
384388
}
385389

386390
if(op.insertMany) {
387391
for(var i = 0; i < op.insertMany.length; i++) {
392+
if(forceServerObjectId !== true && op.insertMany[i]._id == null) op.insertMany[i]._id = new ObjectID();
388393
addToOperationsList(this, common.INSERT, op.insertMany[i]);
389394
}
390395

lib/collection.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -450,8 +450,12 @@ Collection.prototype.insertMany = function(docs, options, callback) {
450450
// If keep going set unordered
451451
options['serializeFunctions'] = options['serializeFunctions'] || self.s.serializeFunctions;
452452

453+
// Set up the force server object id
454+
var forceServerObjectId = typeof options.forceServerObjectId == 'boolean'
455+
? options.forceServerObjectId : self.s.db.options.forceServerObjectId;
456+
453457
// Do we want to force the server to assign the _id key
454-
if(options.forceServerObjectId == null || options.forceServerObjectId == false) {
458+
if(forceServerObjectId !== true) {
455459
// Add _id if not specified
456460
for(var i = 0; i < docs.length; i++) {
457461
if(docs[i]._id == null) docs[i]._id = self.s.pkFactory.createPk();
@@ -631,9 +635,15 @@ var insertDocuments = function(self, docs, options, callback) {
631635
if(finalOptions.keepGoing == true) finalOptions.ordered = false;
632636
finalOptions['serializeFunctions'] = options['serializeFunctions'] || self.s.serializeFunctions;
633637

638+
// Set up the force server object id
639+
var forceServerObjectId = typeof options.forceServerObjectId == 'boolean'
640+
? options.forceServerObjectId : self.s.db.options.forceServerObjectId;
641+
634642
// Add _id if not specified
635-
for(var i = 0; i < docs.length; i++) {
636-
if(docs[i]._id == null) docs[i]._id = self.s.pkFactory.createPk();
643+
if(forceServerObjectId !== true){
644+
for(var i = 0; i < docs.length; i++) {
645+
if(docs[i]._id == null) docs[i]._id = self.s.pkFactory.createPk();
646+
}
637647
}
638648

639649
// File inserts

test/functional/insert_tests.js

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2080,8 +2080,116 @@ exports['should return error on ordered insert with multiple unique key constrai
20802080
}
20812081
}
20822082

2083+
exports['Correctly allow forceServerObjectId for insertOne'] = {
2084+
metadata: { requires: { topology: ['single'] } },
20832085

2086+
// The actual test we wish to run
2087+
test: function(configuration, test) {
2088+
var started = [];
2089+
var succeeded = [];
2090+
var failed = [];
2091+
var callbackTriggered = false;
2092+
2093+
var listener = require('../..').instrument(function(err, instrumentations) {});
2094+
listener.on('started', function(event) {
2095+
if(event.commandName == 'insert')
2096+
started.push(event);
2097+
});
2098+
2099+
listener.on('succeeded', function(event) {
2100+
if(event.commandName == 'insert')
2101+
succeeded.push(event);
2102+
});
2103+
2104+
var db = configuration.newDbInstance({w:1}, {poolSize:1, auto_reconnect:false});
2105+
db.open(function(err, db) {
2106+
test.equal(null, err);
2107+
2108+
db.collection('apm_test').insertOne({a:1}, {forceServerObjectId:true}).then(function(r) {
2109+
test.equal(null, err);
2110+
test.equal(undefined, started[0].command.documents[0]._id);
2111+
listener.uninstrument();
2112+
2113+
db.close();
2114+
test.done();
2115+
});
2116+
});
2117+
}
2118+
}
2119+
2120+
exports['Correctly allow forceServerObjectId for insertMany'] = {
2121+
metadata: { requires: { topology: ['single'] } },
2122+
2123+
// The actual test we wish to run
2124+
test: function(configuration, test) {
2125+
var started = [];
2126+
var succeeded = [];
2127+
var failed = [];
2128+
var callbackTriggered = false;
2129+
2130+
var listener = require('../..').instrument(function(err, instrumentations) {});
2131+
listener.on('started', function(event) {
2132+
if(event.commandName == 'insert')
2133+
started.push(event);
2134+
});
2135+
2136+
listener.on('succeeded', function(event) {
2137+
if(event.commandName == 'insert')
2138+
succeeded.push(event);
2139+
});
2140+
2141+
var db = configuration.newDbInstance({w:1}, {poolSize:1, auto_reconnect:false});
2142+
db.open(function(err, db) {
2143+
test.equal(null, err);
20842144

2145+
db.collection('apm_test').insertMany([{a:1}], {forceServerObjectId:true}).then(function(r) {
2146+
test.equal(null, err);
2147+
test.equal(undefined, started[0].command.documents[0]._id);
2148+
2149+
listener.uninstrument();
2150+
db.close();
2151+
test.done();
2152+
});
2153+
});
2154+
}
2155+
}
2156+
2157+
exports['Correctly allow forceServerObjectId for insertMany'] = {
2158+
metadata: { requires: { topology: ['single'] } },
2159+
2160+
// The actual test we wish to run
2161+
test: function(configuration, test) {
2162+
var started = [];
2163+
var succeeded = [];
2164+
var failed = [];
2165+
var callbackTriggered = false;
2166+
2167+
var listener = require('../..').instrument(function(err, instrumentations) {});
2168+
listener.on('started', function(event) {
2169+
if(event.commandName == 'insert')
2170+
started.push(event);
2171+
});
2172+
2173+
listener.on('succeeded', function(event) {
2174+
if(event.commandName == 'insert')
2175+
succeeded.push(event);
2176+
});
2177+
2178+
var db = configuration.newDbInstance({w:1}, {poolSize:1, auto_reconnect:false});
2179+
db.open(function(err, db) {
2180+
test.equal(null, err);
2181+
2182+
db.collection('apm_test').insertMany([{a:1}], {forceServerObjectId:true}).then(function(r) {
2183+
test.equal(null, err);
2184+
test.equal(undefined, started[0].command.documents[0]._id);
2185+
2186+
listener.uninstrument();
2187+
db.close();
2188+
test.done();
2189+
});
2190+
});
2191+
}
2192+
}
20852193

20862194

20872195

0 commit comments

Comments
 (0)