Skip to content

Commit e4a9a57

Browse files
authoredJun 28, 2021
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

+1
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

+9
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

+11-2
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

+4-1
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

+11-2
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

+4
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

+7
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

+2-1
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

+1
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

+1
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

‎lib/core/topologies/replset.js

+1
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 {number} [options.pingInterval=5000] Ping interval to check the response time to the different servers
9293
* @param {number} [options.localThresholdMS=15] Cutoff latency point in MS for Replicaset member selection
9394
* @param {boolean} [options.domainsEnabled=false] Enable the wrapping of the callback in the current domain, disabled by default to avoid perf hit.

‎lib/core/topologies/server.js

+2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ var debugFields = [
4747
'promoteLongs',
4848
'promoteValues',
4949
'promoteBuffers',
50+
'bsonRegExp',
5051
'servername'
5152
];
5253

@@ -89,6 +90,7 @@ function topologyId(server) {
8990
* @param {boolean} [options.promoteLongs=true] Convert Long values from the db into Numbers if they fit into 53 bits
9091
* @param {boolean} [options.promoteValues=true] Promotes BSON values to native types where possible, set to false to only receive wrapper types.
9192
* @param {boolean} [options.promoteBuffers=false] Promotes Binary BSON values to native Node Buffers.
93+
* @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.
9294
* @param {string} [options.appname=null] Application name, passed in on ismaster call and logged in mongod server logs. Maximum size 128 bytes.
9395
* @param {boolean} [options.domainsEnabled=false] Enable the wrapping of the callback in the current domain, disabled by default to avoid perf hit.
9496
* @param {boolean} [options.monitorCommands=false] Enable command monitoring for this topology

‎lib/core/wireprotocol/shared.js

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ function applyCommonQueryOptions(queryOptions, options) {
5757
promoteLongs: typeof options.promoteLongs === 'boolean' ? options.promoteLongs : true,
5858
promoteValues: typeof options.promoteValues === 'boolean' ? options.promoteValues : true,
5959
promoteBuffers: typeof options.promoteBuffers === 'boolean' ? options.promoteBuffers : false,
60+
bsonRegExp: typeof options.bsonRegExp === 'boolean' ? options.bsonRegExp : false,
6061
monitoring: typeof options.monitoring === 'boolean' ? options.monitoring : false,
6162
fullResult: typeof options.fullResult === 'boolean' ? options.fullResult : false
6263
});

‎lib/db.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ const legalOptionNames = [
8383
'bufferMaxEntries',
8484
'authSource',
8585
'ignoreUndefined',
86-
'promoteLongs',
8786
'promiseLibrary',
8887
'readConcern',
8988
'retryMiliSeconds',
@@ -95,6 +94,7 @@ const legalOptionNames = [
9594
'promoteBuffers',
9695
'promoteLongs',
9796
'promoteValues',
97+
'bsonRegExp',
9898
'compression',
9999
'retryWrites'
100100
];
@@ -117,6 +117,7 @@ const legalOptionNames = [
117117
* @param {boolean} [options.promoteLongs=true] Promotes Long values to number if they fit inside the 53 bits resolution.
118118
* @param {boolean} [options.promoteBuffers=false] Promotes Binary BSON values to native Node Buffers.
119119
* @param {boolean} [options.promoteValues=true] Promotes BSON values to native types where possible, set to false to only receive wrapper types.
120+
* @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.
120121
* @param {number} [options.bufferMaxEntries=-1] Sets a cap on how many operations the driver will buffer up before giving up on getting a working connection, default is -1 which is unlimited.
121122
* @param {(ReadPreference|string)} [options.readPreference] The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
122123
* @param {object} [options.pkFactory] A primary key factory object for generation of custom _id keys.
@@ -323,6 +324,7 @@ Db.prototype.command = function(command, options, callback) {
323324
* @param {boolean} [options.promoteLongs=true] Promotes Long values to number if they fit inside the 53 bits resolution.
324325
* @param {boolean} [options.promoteValues=true] Promotes BSON values to native types where possible, set to false to only receive wrapper types.
325326
* @param {boolean} [options.promoteBuffers=false] Promotes Binary BSON values to native Node Buffers.
327+
* @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.
326328
* @param {object} [options.collation] Specify collation (MongoDB 3.4 or higher) settings for update operation (see 3.4 documentation for available fields).
327329
* @param {string} [options.comment] Add a comment to an aggregation command
328330
* @param {string|object} [options.hint] Add an index selection hint to an aggregation command
@@ -391,7 +393,8 @@ const COLLECTION_OPTION_KEYS = [
391393
'ignoreUndefined',
392394
'promoteValues',
393395
'promoteBuffers',
394-
'promoteLongs'
396+
'promoteLongs',
397+
'bsonRegExp'
395398
];
396399

397400
/**

‎lib/mongo_client.js

+1
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ const validOptions = require('./operations/connect').validOptions;
144144
* @property {boolean} [promoteValues] (**default**: true) Promotes BSON values to native types where possible, set to false to only receive wrapper types
145145
* @property {boolean} [promoteBuffers] (**default**: false) Promotes Binary BSON values to native Node Buffers
146146
* @property {boolean} [promoteLongs] (**default**: true) Promotes long values to number if they fit inside the 53 bits resolution
147+
* * @param {boolean} [bsonRegExp] (**default**: 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.
147148
* @property {boolean} [domainsEnabled] (**default**: false) Enable the wrapping of the callback in the current domain, disabled by default to avoid perf hit
148149
* @property {object} [validateOptions] (**default**: false) Validate MongoClient passed in options for correctness
149150
* @property {string} [appname] (**default**: undefined) The name of the application that created this MongoClient instance. MongoDB 3.4 and newer will print this value in the server log upon establishing each connection. It is also recorded in the slow query log and profile collections

‎lib/operations/command.js

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const debugFields = [
2222
'promoteLongs',
2323
'promoteValues',
2424
'promoteBuffers',
25+
'bsonRegExp',
2526
'bufferMaxEntries',
2627
'numberOfRetries',
2728
'retryMiliSeconds',

‎lib/operations/connect.js

+1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ const validOptionNames = [
122122
'promoteValues',
123123
'promoteBuffers',
124124
'promoteLongs',
125+
'bsonRegExp',
125126
'domainsEnabled',
126127
'checkServerIdentity',
127128
'validateOptions',

‎lib/operations/db_ops.js

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const debugFields = [
2424
'promoteLongs',
2525
'promoteValues',
2626
'promoteBuffers',
27+
'bsonRegExp',
2728
'bufferMaxEntries',
2829
'numberOfRetries',
2930
'retryMiliSeconds',

‎lib/topologies/mongos.js

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ var legalOptionNames = [
5454
'promoteLongs',
5555
'promoteValues',
5656
'promoteBuffers',
57+
'bsonRegExp',
5758
'promiseLibrary',
5859
'monitorCommands'
5960
];

‎lib/topologies/replset.js

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ var legalOptionNames = [
6060
'promoteLongs',
6161
'promoteValues',
6262
'promoteBuffers',
63+
'bsonRegExp',
6364
'maxStalenessSeconds',
6465
'promiseLibrary',
6566
'minSize',

‎lib/topologies/server.js

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ var legalOptionNames = [
5656
'promoteLongs',
5757
'promoteValues',
5858
'promoteBuffers',
59+
'bsonRegExp',
5960
'compression',
6061
'promiseLibrary',
6162
'monitorCommands'

‎test/functional/unified-spec-runner/operations.ts

+2
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ export interface BSONSerializeOptions extends Omit<SerializeOptions, 'index'> {
5454
promoteBuffers?: boolean;
5555
/** Promotes long values to number if they fit inside the 53 bits resolution */
5656
promoteLongs?: boolean;
57+
/** Promotes RegExp to BSONRegExp object if enabled */
58+
bsonRegExp?: boolean;
5759
/** Serialize functions on any object */
5860
serializeFunctions?: boolean;
5961
/** Specify if the BSON serializer should ignore undefined fields */

‎test/unit/bson_regex.test.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'use strict';
2+
3+
const expect = require('chai').expect;
4+
const BSONRegExp = require('bson').BSONRegExp;
5+
6+
describe('BSONRegExp', () => {
7+
describe('bsonRegExp option', () => {
8+
// define option for tests to use
9+
const option = { bsonRegExp: true };
10+
for (const passOptionTo of ['client', 'db', 'collection', 'operation']) {
11+
it(`should respond with BSONRegExp class with option passed to ${passOptionTo}`, function(done) {
12+
const client = this.configuration.newClient(passOptionTo === 'client' ? option : undefined);
13+
let collection;
14+
15+
client
16+
.connect()
17+
.then(() => {
18+
const db = client.db('bson_regex_db', passOptionTo === 'db' ? option : undefined);
19+
collection = db.collection(
20+
'bson_regex_coll',
21+
passOptionTo === 'collection' ? option : undefined
22+
);
23+
})
24+
.then(() => collection.insertOne({ regex: new BSONRegExp('abc', 'imx') }))
25+
.then(() =>
26+
collection.findOne(
27+
{ regex: new BSONRegExp('abc', 'imx') },
28+
passOptionTo === 'operation' ? option : undefined
29+
)
30+
)
31+
.then(res =>
32+
expect(res)
33+
.has.property('regex')
34+
.that.is.instanceOf(BSONRegExp)
35+
)
36+
.then(
37+
() => client.close(done),
38+
err => client.close(() => done(err))
39+
);
40+
});
41+
}
42+
});
43+
});

0 commit comments

Comments
 (0)
Please sign in to comment.