Skip to content

Commit aabbfae

Browse files
committed
Added fix to allow for per operation forceServerId
1 parent aa094fc commit aabbfae

File tree

4 files changed

+128
-4
lines changed

4 files changed

+128
-4
lines changed

lib/bulk/ordered.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,11 @@ var define = OrderedBulkOperation.define = new Define('OrderedBulkOperation', Or
293293

294294
OrderedBulkOperation.prototype.raw = function(op) {
295295
var key = Object.keys(op)[0];
296-
var forceServerObjectId = this.s.collection.s.db.options.forceServerObjectId;
296+
297+
// Set up the force server object id
298+
var forceServerObjectId = typeof this.s.options.forceServerObjectId == 'boolean'
299+
? this.s.options.forceServerObjectId : this.s.collection.s.db.options.forceServerObjectId;
300+
297301
// Update operations
298302
if((op.updateOne && op.updateOne.q)
299303
|| (op.updateMany && op.updateMany.q)

lib/bulk/unordered.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,11 @@ Object.defineProperty(UnorderedBulkOperation.prototype, 'length', {
342342

343343
UnorderedBulkOperation.prototype.raw = function(op) {
344344
var key = Object.keys(op)[0];
345-
var forceServerObjectId = this.s.collection.s.db.options.forceServerObjectId;
345+
346+
// Set up the force server object id
347+
var forceServerObjectId = typeof this.s.options.forceServerObjectId == 'boolean'
348+
? this.s.options.forceServerObjectId : this.s.collection.s.db.options.forceServerObjectId;
349+
346350
// Update operations
347351
if((op.updateOne && op.updateOne.q)
348352
|| (op.updateMany && op.updateMany.q)

lib/collection.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -437,8 +437,12 @@ Collection.prototype.insertMany = function(docs, options, callback) {
437437
// If keep going set unordered
438438
options['serializeFunctions'] = options['serializeFunctions'] || self.s.serializeFunctions;
439439

440+
// Set up the force server object id
441+
var forceServerObjectId = typeof options.forceServerObjectId == 'boolean'
442+
? options.forceServerObjectId : self.s.db.options.forceServerObjectId;
443+
440444
// Do we want to force the server to assign the _id key
441-
if(self.s.db.options.forceServerObjectId!==true) {
445+
if(forceServerObjectId !== true) {
442446
// Add _id if not specified
443447
for(var i = 0; i < docs.length; i++) {
444448
if(docs[i]._id == null) docs[i]._id = self.s.pkFactory.createPk();
@@ -617,8 +621,12 @@ var insertDocuments = function(self, docs, options, callback) {
617621
if(finalOptions.keepGoing == true) finalOptions.ordered = false;
618622
finalOptions['serializeFunctions'] = options['serializeFunctions'] || self.s.serializeFunctions;
619623

624+
// Set up the force server object id
625+
var forceServerObjectId = typeof options.forceServerObjectId == 'boolean'
626+
? options.forceServerObjectId : self.s.db.options.forceServerObjectId;
627+
620628
// Add _id if not specified
621-
if(self.s.db.options.forceServerObjectId!==true){
629+
if(forceServerObjectId !== true){
622630
for(var i = 0; i < docs.length; i++) {
623631
if(docs[i]._id == null) docs[i]._id = self.s.pkFactory.createPk();
624632
}

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)