Skip to content

Commit e4a9a57

Browse files
authored
fix(NODE-3150): added bsonRegExp option for v3.6 (#2843)
1 parent 750760c commit e4a9a57

File tree

23 files changed

+112
-8
lines changed

23 files changed

+112
-8
lines changed

lib/cmap/connection.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ function write(command, options, callback) {
317317
promoteLongs: typeof options.promoteLongs === 'boolean' ? options.promoteLongs : true,
318318
promoteValues: typeof options.promoteValues === 'boolean' ? options.promoteValues : true,
319319
promoteBuffers: typeof options.promoteBuffers === 'boolean' ? options.promoteBuffers : false,
320+
bsonRegExp: typeof options.bsonRegExp === 'boolean' ? options.bsonRegExp : false,
320321
raw: typeof options.raw === 'boolean' ? options.raw : false
321322
};
322323

lib/collection.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ function Collection(db, topology, dbName, name, pkFactory, options) {
120120
options == null || options.promoteBuffers == null
121121
? db.s.options.promoteBuffers
122122
: options.promoteBuffers;
123+
const bsonRegExp =
124+
options == null || options.bsonRegExp == null ? db.s.options.bsonRegExp : options.bsonRegExp;
123125
const collectionHint = null;
124126

125127
const namespace = new MongoDBNamespace(dbName, name);
@@ -156,6 +158,8 @@ function Collection(db, topology, dbName, name, pkFactory, options) {
156158
promoteValues: promoteValues,
157159
// promoteBuffers
158160
promoteBuffers: promoteBuffers,
161+
// bsonRegExp
162+
bsonRegExp: bsonRegExp,
159163
// internalHint
160164
internalHint: internalHint,
161165
// collectionHint
@@ -303,6 +307,7 @@ const DEPRECATED_FIND_OPTIONS = ['maxScan', 'fields', 'snapshot', 'oplogReplay']
303307
* @param {boolean} [options.promoteLongs=true] Promotes Long values to number if they fit inside the 53 bits resolution.
304308
* @param {boolean} [options.promoteValues=true] Promotes BSON values to native types where possible, set to false to only receive wrapper types.
305309
* @param {boolean} [options.promoteBuffers=false] Promotes Binary BSON values to native Node Buffers.
310+
* @param {boolean} [options.bsonRegExp=false] By default, regex returned from MDB will be native to the language. Setting to true will ensure that a BSON.BSONRegExp object is returned.
306311
* @param {(ReadPreference|string)} [options.readPreference] The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
307312
* @param {boolean} [options.partial=false] Specify if the cursor should return partial results when querying against a sharded system
308313
* @param {number} [options.maxTimeMS] Number of milliseconds to wait before aborting the query.
@@ -451,6 +456,8 @@ Collection.prototype.find = deprecateOptions(
451456
newOptions.promoteValues = this.s.promoteValues;
452457
if (newOptions.promoteBuffers == null && typeof this.s.promoteBuffers === 'boolean')
453458
newOptions.promoteBuffers = this.s.promoteBuffers;
459+
if (newOptions.bsonRegExp == null && typeof this.s.bsonRegExp === 'boolean')
460+
newOptions.bsonRegExp = this.s.bsonRegExp;
454461

455462
// Sort options
456463
if (findCommand.sort) {
@@ -1075,6 +1082,7 @@ Collection.prototype.save = deprecate(function(doc, options, callback) {
10751082
* @param {boolean} [options.promoteLongs=true] Promotes Long values to number if they fit inside the 53 bits resolution.
10761083
* @param {boolean} [options.promoteValues=true] Promotes BSON values to native types where possible, set to false to only receive wrapper types.
10771084
* @param {boolean} [options.promoteBuffers=false] Promotes Binary BSON values to native Node Buffers.
1085+
* @param {boolean} [options.bsonRegExp=false] By default, regex returned from MDB will be native to the language. Setting to true will ensure that a BSON.BSONRegExp object is returned.
10781086
* @param {(ReadPreference|string)} [options.readPreference] The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
10791087
* @param {boolean} [options.partial=false] Specify if the cursor should return partial results when querying against a sharded system
10801088
* @param {number} [options.maxTimeMS] Number of milliseconds to wait before aborting the query.
@@ -1899,6 +1907,7 @@ Collection.prototype.findAndRemove = deprecate(function(query, sort, options, ca
18991907
* @param {boolean} [options.promoteLongs=true] Promotes Long values to number if they fit inside the 53 bits resolution.
19001908
* @param {boolean} [options.promoteValues=true] Promotes BSON values to native types where possible, set to false to only receive wrapper types.
19011909
* @param {boolean} [options.promoteBuffers=false] Promotes Binary BSON values to native Node Buffers.
1910+
* @param {boolean} [options.bsonRegExp=false] By default, regex returned from MDB will be native to the language. Setting to true will ensure that a BSON.BSONRegExp object is returned.
19021911
* @param {object} [options.collation] Specify collation settings for operation. See {@link https://docs.mongodb.com/manual/reference/command/aggregate|aggregation documentation}.
19031912
* @param {string} [options.comment] Add a comment to an aggregation command
19041913
* @param {string|object} [options.hint] Add an index selection hint to an aggregation command

lib/core/connection/commands.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,12 @@ KillCursor.prototype.toBin = function() {
398398
};
399399

400400
var Response = function(bson, message, msgHeader, msgBody, opts) {
401-
opts = opts || { promoteLongs: true, promoteValues: true, promoteBuffers: false };
401+
opts = opts || {
402+
promoteLongs: true,
403+
promoteValues: true,
404+
promoteBuffers: false,
405+
bsonRegExp: false
406+
};
402407
this.parsed = false;
403408
this.raw = message;
404409
this.data = msgBody;
@@ -429,6 +434,7 @@ var Response = function(bson, message, msgHeader, msgBody, opts) {
429434
this.promoteLongs = typeof opts.promoteLongs === 'boolean' ? opts.promoteLongs : true;
430435
this.promoteValues = typeof opts.promoteValues === 'boolean' ? opts.promoteValues : true;
431436
this.promoteBuffers = typeof opts.promoteBuffers === 'boolean' ? opts.promoteBuffers : false;
437+
this.bsonRegExp = typeof opts.bsonRegExp === 'boolean' ? opts.bsonRegExp : false;
432438
};
433439

434440
Response.prototype.isParsed = function() {
@@ -449,13 +455,16 @@ Response.prototype.parse = function(options) {
449455
typeof options.promoteValues === 'boolean' ? options.promoteValues : this.opts.promoteValues;
450456
var promoteBuffers =
451457
typeof options.promoteBuffers === 'boolean' ? options.promoteBuffers : this.opts.promoteBuffers;
458+
var bsonRegExp =
459+
typeof options.bsonRegExp === 'boolean' ? options.bsonRegExp : this.opts.bsonRegExp;
452460
var bsonSize, _options;
453461

454462
// Set up the options
455463
_options = {
456464
promoteLongs: promoteLongs,
457465
promoteValues: promoteValues,
458-
promoteBuffers: promoteBuffers
466+
promoteBuffers: promoteBuffers,
467+
bsonRegExp: bsonRegExp
459468
};
460469

461470
// Position within OP_REPLY at which documents start

lib/core/connection/connection.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const DEBUG_FIELDS = [
3838
'promoteLongs',
3939
'promoteValues',
4040
'promoteBuffers',
41+
'bsonRegExp',
4142
'checkServerIdentity'
4243
];
4344

@@ -73,6 +74,7 @@ class Connection extends EventEmitter {
7374
* @param {boolean} [options.promoteLongs] Convert Long values from the db into Numbers if they fit into 53 bits
7475
* @param {boolean} [options.promoteValues] Promotes BSON values to native types where possible, set to false to only receive wrapper types.
7576
* @param {boolean} [options.promoteBuffers] Promotes Binary BSON values to native Node Buffers.
77+
* @param {boolean} [options.bsonRegExp] By default, regex returned from MDB will be native to the language. Setting to true will ensure that a BSON.BSONRegExp object is returned.
7678
* @param {number} [options.maxBsonMessageSize=0x4000000] Largest possible size of a BSON message (for legacy purposes)
7779
*/
7880
constructor(socket, options) {
@@ -117,7 +119,8 @@ class Connection extends EventEmitter {
117119
this.responseOptions = {
118120
promoteLongs: typeof options.promoteLongs === 'boolean' ? options.promoteLongs : true,
119121
promoteValues: typeof options.promoteValues === 'boolean' ? options.promoteValues : true,
120-
promoteBuffers: typeof options.promoteBuffers === 'boolean' ? options.promoteBuffers : false
122+
promoteBuffers: typeof options.promoteBuffers === 'boolean' ? options.promoteBuffers : false,
123+
bsonRegExp: typeof options.bsonRegExp === 'boolean' ? options.bsonRegExp : false
121124
};
122125

123126
// Flushing

lib/core/connection/msg.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,12 @@ Msg.getRequestId = function() {
139139

140140
class BinMsg {
141141
constructor(bson, message, msgHeader, msgBody, opts) {
142-
opts = opts || { promoteLongs: true, promoteValues: true, promoteBuffers: false };
142+
opts = opts || {
143+
promoteLongs: true,
144+
promoteValues: true,
145+
promoteBuffers: false,
146+
bsonRegExp: false
147+
};
143148
this.parsed = false;
144149
this.raw = message;
145150
this.data = msgBody;
@@ -161,6 +166,7 @@ class BinMsg {
161166
this.promoteLongs = typeof opts.promoteLongs === 'boolean' ? opts.promoteLongs : true;
162167
this.promoteValues = typeof opts.promoteValues === 'boolean' ? opts.promoteValues : true;
163168
this.promoteBuffers = typeof opts.promoteBuffers === 'boolean' ? opts.promoteBuffers : false;
169+
this.bsonRegExp = typeof opts.bsonRegExp === 'boolean' ? opts.bsonRegExp : false;
164170

165171
this.documents = [];
166172
}
@@ -186,12 +192,15 @@ class BinMsg {
186192
typeof options.promoteBuffers === 'boolean'
187193
? options.promoteBuffers
188194
: this.opts.promoteBuffers;
195+
const bsonRegExp =
196+
typeof options.bsonRegExp === 'boolean' ? options.bsonRegExp : this.opts.bsonRegExp;
189197

190198
// Set up the options
191199
const _options = {
192200
promoteLongs: promoteLongs,
193201
promoteValues: promoteValues,
194-
promoteBuffers: promoteBuffers
202+
promoteBuffers: promoteBuffers,
203+
bsonRegExp: bsonRegExp
195204
};
196205

197206
while (this.index < this.data.length) {

lib/core/connection/pool.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ var _id = 0;
7676
* @param {boolean} [options.promoteLongs=true] Convert Long values from the db into Numbers if they fit into 53 bits
7777
* @param {boolean} [options.promoteValues=true] Promotes BSON values to native types where possible, set to false to only receive wrapper types.
7878
* @param {boolean} [options.promoteBuffers=false] Promotes Binary BSON values to native Node Buffers.
79+
* @param {boolean} [options.bsonRegExp=false] By default, regex returned from MDB will be native to the language. Setting to true will ensure that a BSON.BSONRegExp object is returned.
7980
* @param {boolean} [options.domainsEnabled=false] Enable the wrapping of the callback in the current domain, disabled by default to avoid perf hit.
8081
* @fires Pool#connect
8182
* @fires Pool#close
@@ -127,6 +128,7 @@ var Pool = function(topology, options) {
127128
promoteLongs: true,
128129
promoteValues: true,
129130
promoteBuffers: false,
131+
bsonRegExp: false,
130132
// Reconnection options
131133
reconnect: true,
132134
reconnectInterval: 1000,
@@ -870,6 +872,7 @@ Pool.prototype.write = function(command, options, cb) {
870872
promoteLongs: true,
871873
promoteValues: true,
872874
promoteBuffers: false,
875+
bsonRegExp: false,
873876
fullResult: false
874877
};
875878

@@ -879,6 +882,7 @@ Pool.prototype.write = function(command, options, cb) {
879882
typeof options.promoteValues === 'boolean' ? options.promoteValues : true;
880883
operation.promoteBuffers =
881884
typeof options.promoteBuffers === 'boolean' ? options.promoteBuffers : false;
885+
operation.bsonRegExp = typeof options.bsonRegExp === 'boolean' ? options.bsonRegExp : false;
882886
operation.raw = typeof options.raw === 'boolean' ? options.raw : false;
883887
operation.immediateRelease =
884888
typeof options.immediateRelease === 'boolean' ? options.immediateRelease : false;

lib/core/cursor.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,13 @@ class CoreCursor extends Readable {
146146
this.cursorState.promoteBuffers = options.promoteBuffers;
147147
}
148148

149+
// Add bsonRegExp to cursor state
150+
if (typeof topologyOptions.bsonRegExp === 'boolean') {
151+
this.cursorState.bsonRegExp = topologyOptions.bsonRegExp;
152+
} else if (typeof options.bsonRegExp === 'boolean') {
153+
this.cursorState.bsonRegExp = options.bsonRegExp;
154+
}
155+
149156
if (topologyOptions.reconnect) {
150157
this.cursorState.reconnect = topologyOptions.reconnect;
151158
}

lib/core/sdam/monitor.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ class Monitor extends EventEmitter {
8585
raw: false,
8686
promoteLongs: true,
8787
promoteValues: true,
88-
promoteBuffers: true
88+
promoteBuffers: true,
89+
bsonRegExp: true
8990
}
9091
);
9192

lib/core/sdam/server.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ const DEBUG_FIELDS = [
5050
'promoteLongs',
5151
'promoteValues',
5252
'promoteBuffers',
53+
'bsonRegExp',
5354
'servername'
5455
];
5556

lib/core/topologies/mongos.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ var handlers = ['connect', 'close', 'error', 'timeout', 'parseError'];
8888
* @param {boolean} [options.promoteLongs=true] Convert Long values from the db into Numbers if they fit into 53 bits
8989
* @param {boolean} [options.promoteValues=true] Promotes BSON values to native types where possible, set to false to only receive wrapper types.
9090
* @param {boolean} [options.promoteBuffers=false] Promotes Binary BSON values to native Node Buffers.
91+
* @param {boolean} [options.bsonRegExp=false] By default, regex returned from MDB will be native to the language. Setting to true will ensure that a BSON.BSONRegExp object is returned.
9192
* @param {boolean} [options.domainsEnabled=false] Enable the wrapping of the callback in the current domain, disabled by default to avoid perf hit.
9293
* @param {boolean} [options.monitorCommands=false] Enable command monitoring for this topology
9394
* @return {Mongos} A cursor instance

0 commit comments

Comments
 (0)