Skip to content

Commit 8b397f1

Browse files
committed
NODE-800 Added promoteValues flag (default to true) to allow user to specify they only want wrapped BSON values back instead of promotion to native types.
1 parent f5217bc commit 8b397f1

File tree

10 files changed

+132
-8
lines changed

10 files changed

+132
-8
lines changed

HISTORY.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
2.2.8 2016-08-23
2+
----------------
3+
* Updated mongodb-core to 2.0.10.
4+
* Added promoteValues flag (default to true) to allow user to specify they only want wrapped BSON values back instead of promotion to native types.
5+
* Do not close mongos proxy connection on failed ismaster check in ha process (Issue #130).
6+
17
2.2.7 2016-08-19
28
----------------
39
* If only a single mongos is provided in the seedlist, fix issue where it would be assigned as single standalone server instead of mongos topology (Issue #130).

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ connect.Code = core.BSON.Code;
3232
connect.Map = core.BSON.Map;
3333
connect.DBRef = core.BSON.DBRef;
3434
connect.Double = core.BSON.Double;
35+
connect.Int32 = core.BSON.Int32;
3536
connect.Long = core.BSON.Long;
3637
connect.MinKey = core.BSON.MinKey;
3738
connect.MaxKey = core.BSON.MaxKey;

lib/collection.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ var Collection = function(db, topology, dbName, name, pkFactory, options) {
6565
var slaveOk = options == null || options.slaveOk == null ? db.slaveOk : options.slaveOk;
6666
var serializeFunctions = options == null || options.serializeFunctions == null ? db.s.options.serializeFunctions : options.serializeFunctions;
6767
var raw = options == null || options.raw == null ? db.s.options.raw : options.raw;
68-
var promoteLongs = options == null || options.raw == null ? db.s.options.promoteLongs : options.promoteLongs;
68+
var promoteLongs = options == null || options.promoteLongs == null ? db.s.options.promoteLongs : options.promoteLongs;
69+
var promoteValues = options == null || options.raw == promoteValues ? db.s.options.promoteValues : options.promoteValues;
6970
var readPreference = null;
7071
var collectionHint = null;
7172
var namespace = f("%s.%s", dbName, name);
@@ -115,6 +116,8 @@ var Collection = function(db, topology, dbName, name, pkFactory, options) {
115116
, raw: raw
116117
// promoteLongs
117118
, promoteLongs: promoteLongs
119+
// promoteValues
120+
, promoteValues: promoteValues
118121
// internalHint
119122
, internalHint: internalHint
120123
// collectionHint
@@ -347,6 +350,7 @@ Collection.prototype.find = function() {
347350
if(newOptions.raw == null && typeof this.s.raw == 'boolean') newOptions.raw = this.s.raw;
348351
// Set promoteLongs if available at collection level
349352
if(newOptions.promoteLongs == null && typeof this.s.promoteLongs == 'boolean') newOptions.promoteLongs = this.s.promoteLongs;
353+
if(newOptions.promoteValues == null && typeof this.s.promoteValues == 'boolean') newOptions.promoteValues = this.s.promoteValues;
350354

351355
// Sort options
352356
if(findCommand.sort) {

lib/db.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ var EventEmitter = require('events').EventEmitter
2424
, assign = require('./utils').assign;
2525

2626
var debugFields = ['authSource', 'w', 'wtimeout', 'j', 'native_parser', 'forceServerObjectId'
27-
, 'serializeFunctions', 'raw', 'promoteLongs', 'bufferMaxEntries', 'numberOfRetries', 'retryMiliSeconds'
27+
, 'serializeFunctions', 'raw', 'promoteLongs', 'promoteValues', 'bufferMaxEntries', 'numberOfRetries', 'retryMiliSeconds'
2828
, 'readPreference', 'pkFactory', 'parentDb', 'promiseLibrary', 'noListener'];
2929

3030
/**
@@ -64,6 +64,7 @@ var legalOptionNames = ['w', 'wtimeout', 'fsync', 'j', 'readPreference', 'readPr
6464
* @param {Boolean} [options.ignoreUndefined=false] Specify if the BSON serializer should ignore undefined fields.
6565
* @param {boolean} [options.raw=false] Return document results as raw BSON buffers.
6666
* @param {boolean} [options.promoteLongs=true] Promotes Long values to number if they fit inside the 53 bits resolution.
67+
* @param {boolean} [options.promoteValues=true] Promotes BSON values to native types where possible, set to false to only receive wrapper types.
6768
* @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.
6869
* @param {(ReadPreference|string)} [options.readPreference=null] The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
6970
* @param {object} [options.pkFactory=null] A primary key factory object for generation of custom _id keys.

lib/mongos.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ var release = os.release();
5454
, 'store', 'auto_reconnect', 'autoReconnect', 'emitError'
5555
, 'keepAlive', 'noDelay', 'connectTimeoutMS', 'socketTimeoutMS'
5656
, 'loggerLevel', 'logger', 'reconnectTries', 'appname', 'domainsEnabled'
57-
, 'servername', 'promoteLongs'];
57+
, 'servername', 'promoteLongs', 'promoteValues'];
5858

5959
/**
6060
* Creates a new Mongos instance

lib/replset.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ var legalOptionNames = ['ha', 'haInterval', 'replicaSet', 'rs_name', 'secondaryA
4949
, 'store', 'auto_reconnect', 'autoReconnect', 'emitError'
5050
, 'keepAlive', 'noDelay', 'connectTimeoutMS', 'socketTimeoutMS', 'strategy', 'debug'
5151
, 'loggerLevel', 'logger', 'reconnectTries', 'appname', 'domainsEnabled'
52-
, 'servername', 'promoteLongs'];
52+
, 'servername', 'promoteLongs', 'promoteValues'];
5353

5454
// Get package.json variable
5555
var driverVersion = require(__dirname + '/../package.json').version;

lib/server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ var release = os.release();
5151
, 'keepAlive', 'noDelay', 'connectTimeoutMS', 'socketTimeoutMS'
5252
, 'loggerLevel', 'logger', 'reconnectTries', 'reconnectInterval', 'monitoring'
5353
, 'appname', 'domainsEnabled'
54-
, 'servername', 'promoteLongs'];
54+
, 'servername', 'promoteLongs', 'promoteValues'];
5555

5656
/**
5757
* Creates a new Server instance

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mongodb",
3-
"version": "2.2.7",
3+
"version": "2.2.8",
44
"description": "The official MongoDB driver for Node.js",
55
"main": "index.js",
66
"repository": {
@@ -14,8 +14,7 @@
1414
],
1515
"dependencies": {
1616
"es6-promise": "3.2.1",
17-
"mongodb-core": "2.0.9",
18-
"mongodb-core": "christkv/mongodb-core#2.0",
17+
"mongodb-core": "2.0.10",
1918
"readable-stream": "2.1.5"
2019
},
2120
"devDependencies": {
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
exports['should correctly honor promoteValues when creating an instance using Db'] = {
2+
// Add a tag that our runner can trigger on
3+
// in this case we are setting that node needs to be higher than 0.10.X to run
4+
metadata: { requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] } },
5+
6+
// The actual test we wish to run
7+
test: function(configuration, test) {
8+
var Long = configuration.require.Long,
9+
Int32 = configuration.require.Int32,
10+
Double = configuration.require.Double;
11+
12+
var o = configuration.writeConcernMax();
13+
var db = configuration.newDbInstance(o, {native_parser:true, promoteValues: false})
14+
db.open(function(err, db) {
15+
db.collection('shouldCorrectlyHonorPromoteValues').insert({
16+
doc: Long.fromNumber(10)
17+
, int: 10
18+
, double: 2.2222
19+
, array: [[Long.fromNumber(10)]]
20+
}, function(err, doc) {
21+
test.equal(null, err);
22+
23+
db.collection('shouldCorrectlyHonorPromoteValues').findOne(function(err, doc) {
24+
test.equal(null, err);
25+
26+
test.deepEqual(Long.fromNumber(10), doc.doc);
27+
test.deepEqual(new Int32(10), doc.int);
28+
test.deepEqual(new Double(2.2222), doc.double);
29+
30+
db.close();
31+
test.done();
32+
});
33+
});
34+
});
35+
}
36+
}
37+
38+
exports['should correctly honor promoteValues when creating an instance using MongoClient'] = {
39+
// Add a tag that our runner can trigger on
40+
// in this case we are setting that node needs to be higher than 0.10.X to run
41+
metadata: { requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] } },
42+
43+
// The actual test we wish to run
44+
test: function(configuration, test) {
45+
var Long = configuration.require.Long,
46+
Int32 = configuration.require.Int32,
47+
Double = configuration.require.Double,
48+
MongoClient = configuration.require.MongoClient;
49+
50+
MongoClient.connect(configuration.url(), {
51+
promoteValues: false,
52+
}, function(err, db) {
53+
db.collection('shouldCorrectlyHonorPromoteValues').insert({
54+
doc: Long.fromNumber(10)
55+
, int: 10
56+
, double: 2.2222
57+
, array: [[Long.fromNumber(10)]]
58+
}, function(err, doc) {
59+
test.equal(null, err);
60+
61+
db.collection('shouldCorrectlyHonorPromoteValues').findOne(function(err, doc) {
62+
test.equal(null, err);
63+
64+
test.deepEqual(Long.fromNumber(10), doc.doc);
65+
test.deepEqual(new Int32(10), doc.int);
66+
test.deepEqual(new Double(2.2222), doc.double);
67+
68+
db.close();
69+
test.done();
70+
});
71+
});
72+
});
73+
}
74+
}
75+
76+
exports['should correctly honor promoteValues at cursor level'] = {
77+
// Add a tag that our runner can trigger on
78+
// in this case we are setting that node needs to be higher than 0.10.X to run
79+
metadata: { requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] } },
80+
81+
// The actual test we wish to run
82+
test: function(configuration, test) {
83+
var Long = configuration.require.Long,
84+
Int32 = configuration.require.Int32,
85+
Double = configuration.require.Double,
86+
MongoClient = configuration.require.MongoClient;
87+
88+
MongoClient.connect(configuration.url(), {
89+
promoteValues: false,
90+
}, function(err, db) {
91+
db.collection('shouldCorrectlyHonorPromoteValues').insert({
92+
doc: Long.fromNumber(10)
93+
, int: 10
94+
, double: 2.2222
95+
, array: [[Long.fromNumber(10)]]
96+
}, function(err, doc) {
97+
test.equal(null, err);
98+
99+
db.collection('shouldCorrectlyHonorPromoteValues').find().next(function(err, doc) {
100+
test.equal(null, err);
101+
102+
test.deepEqual(Long.fromNumber(10), doc.doc);
103+
test.deepEqual(new Int32(10), doc.int);
104+
test.deepEqual(new Double(2.2222), doc.double);
105+
106+
db.close();
107+
test.done();
108+
});
109+
});
110+
});
111+
}
112+
}

test/runner.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ var testFiles = [
300300
, '/test/functional/find_and_modify_tests.js'
301301
, '/test/functional/hang_tests.js',
302302
, '/test/functional/disconnect_handler_tests.js',
303+
, '/test/functional/promote_values_tests.js',
303304

304305
// Replicaset tests
305306
, '/test/functional/replset_read_preference_tests.js'

0 commit comments

Comments
 (0)