Skip to content

Commit

Permalink
NODE-800 Added promoteValues flag (default to true) to allow user to …
Browse files Browse the repository at this point in the history
…specify they only want wrapped BSON values back instead of promotion to native types.
  • Loading branch information
christkv committed Aug 23, 2016
1 parent f5217bc commit 8b397f1
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 8 deletions.
6 changes: 6 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
2.2.8 2016-08-23
----------------
* Updated mongodb-core to 2.0.10.
* Added promoteValues flag (default to true) to allow user to specify they only want wrapped BSON values back instead of promotion to native types.
* Do not close mongos proxy connection on failed ismaster check in ha process (Issue #130).

2.2.7 2016-08-19
----------------
* 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).
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ connect.Code = core.BSON.Code;
connect.Map = core.BSON.Map;
connect.DBRef = core.BSON.DBRef;
connect.Double = core.BSON.Double;
connect.Int32 = core.BSON.Int32;
connect.Long = core.BSON.Long;
connect.MinKey = core.BSON.MinKey;
connect.MaxKey = core.BSON.MaxKey;
Expand Down
6 changes: 5 additions & 1 deletion lib/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ var Collection = function(db, topology, dbName, name, pkFactory, options) {
var slaveOk = options == null || options.slaveOk == null ? db.slaveOk : options.slaveOk;
var serializeFunctions = options == null || options.serializeFunctions == null ? db.s.options.serializeFunctions : options.serializeFunctions;
var raw = options == null || options.raw == null ? db.s.options.raw : options.raw;
var promoteLongs = options == null || options.raw == null ? db.s.options.promoteLongs : options.promoteLongs;
var promoteLongs = options == null || options.promoteLongs == null ? db.s.options.promoteLongs : options.promoteLongs;
var promoteValues = options == null || options.raw == promoteValues ? db.s.options.promoteValues : options.promoteValues;
var readPreference = null;
var collectionHint = null;
var namespace = f("%s.%s", dbName, name);
Expand Down Expand Up @@ -115,6 +116,8 @@ var Collection = function(db, topology, dbName, name, pkFactory, options) {
, raw: raw
// promoteLongs
, promoteLongs: promoteLongs
// promoteValues
, promoteValues: promoteValues
// internalHint
, internalHint: internalHint
// collectionHint
Expand Down Expand Up @@ -347,6 +350,7 @@ Collection.prototype.find = function() {
if(newOptions.raw == null && typeof this.s.raw == 'boolean') newOptions.raw = this.s.raw;
// Set promoteLongs if available at collection level
if(newOptions.promoteLongs == null && typeof this.s.promoteLongs == 'boolean') newOptions.promoteLongs = this.s.promoteLongs;
if(newOptions.promoteValues == null && typeof this.s.promoteValues == 'boolean') newOptions.promoteValues = this.s.promoteValues;

// Sort options
if(findCommand.sort) {
Expand Down
3 changes: 2 additions & 1 deletion lib/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var EventEmitter = require('events').EventEmitter
, assign = require('./utils').assign;

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

/**
Expand Down Expand Up @@ -64,6 +64,7 @@ var legalOptionNames = ['w', 'wtimeout', 'fsync', 'j', 'readPreference', 'readPr
* @param {Boolean} [options.ignoreUndefined=false] Specify if the BSON serializer should ignore undefined fields.
* @param {boolean} [options.raw=false] Return document results as raw BSON buffers.
* @param {boolean} [options.promoteLongs=true] Promotes Long values to number if they fit inside the 53 bits resolution.
* @param {boolean} [options.promoteValues=true] Promotes BSON values to native types where possible, set to false to only receive wrapper types.
* @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.
* @param {(ReadPreference|string)} [options.readPreference=null] The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
* @param {object} [options.pkFactory=null] A primary key factory object for generation of custom _id keys.
Expand Down
2 changes: 1 addition & 1 deletion lib/mongos.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ var release = os.release();
, 'store', 'auto_reconnect', 'autoReconnect', 'emitError'
, 'keepAlive', 'noDelay', 'connectTimeoutMS', 'socketTimeoutMS'
, 'loggerLevel', 'logger', 'reconnectTries', 'appname', 'domainsEnabled'
, 'servername', 'promoteLongs'];
, 'servername', 'promoteLongs', 'promoteValues'];

/**
* Creates a new Mongos instance
Expand Down
2 changes: 1 addition & 1 deletion lib/replset.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ var legalOptionNames = ['ha', 'haInterval', 'replicaSet', 'rs_name', 'secondaryA
, 'store', 'auto_reconnect', 'autoReconnect', 'emitError'
, 'keepAlive', 'noDelay', 'connectTimeoutMS', 'socketTimeoutMS', 'strategy', 'debug'
, 'loggerLevel', 'logger', 'reconnectTries', 'appname', 'domainsEnabled'
, 'servername', 'promoteLongs'];
, 'servername', 'promoteLongs', 'promoteValues'];

// Get package.json variable
var driverVersion = require(__dirname + '/../package.json').version;
Expand Down
2 changes: 1 addition & 1 deletion lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ var release = os.release();
, 'keepAlive', 'noDelay', 'connectTimeoutMS', 'socketTimeoutMS'
, 'loggerLevel', 'logger', 'reconnectTries', 'reconnectInterval', 'monitoring'
, 'appname', 'domainsEnabled'
, 'servername', 'promoteLongs'];
, 'servername', 'promoteLongs', 'promoteValues'];

/**
* Creates a new Server instance
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mongodb",
"version": "2.2.7",
"version": "2.2.8",
"description": "The official MongoDB driver for Node.js",
"main": "index.js",
"repository": {
Expand All @@ -14,8 +14,7 @@
],
"dependencies": {
"es6-promise": "3.2.1",
"mongodb-core": "2.0.9",
"mongodb-core": "christkv/mongodb-core#2.0",
"mongodb-core": "2.0.10",
"readable-stream": "2.1.5"
},
"devDependencies": {
Expand Down
112 changes: 112 additions & 0 deletions test/functional/promote_values_tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
exports['should correctly honor promoteValues when creating an instance using Db'] = {
// Add a tag that our runner can trigger on
// in this case we are setting that node needs to be higher than 0.10.X to run
metadata: { requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] } },

// The actual test we wish to run
test: function(configuration, test) {
var Long = configuration.require.Long,
Int32 = configuration.require.Int32,
Double = configuration.require.Double;

var o = configuration.writeConcernMax();
var db = configuration.newDbInstance(o, {native_parser:true, promoteValues: false})
db.open(function(err, db) {
db.collection('shouldCorrectlyHonorPromoteValues').insert({
doc: Long.fromNumber(10)
, int: 10
, double: 2.2222
, array: [[Long.fromNumber(10)]]
}, function(err, doc) {
test.equal(null, err);

db.collection('shouldCorrectlyHonorPromoteValues').findOne(function(err, doc) {
test.equal(null, err);

test.deepEqual(Long.fromNumber(10), doc.doc);
test.deepEqual(new Int32(10), doc.int);
test.deepEqual(new Double(2.2222), doc.double);

db.close();
test.done();
});
});
});
}
}

exports['should correctly honor promoteValues when creating an instance using MongoClient'] = {
// Add a tag that our runner can trigger on
// in this case we are setting that node needs to be higher than 0.10.X to run
metadata: { requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] } },

// The actual test we wish to run
test: function(configuration, test) {
var Long = configuration.require.Long,
Int32 = configuration.require.Int32,
Double = configuration.require.Double,
MongoClient = configuration.require.MongoClient;

MongoClient.connect(configuration.url(), {
promoteValues: false,
}, function(err, db) {
db.collection('shouldCorrectlyHonorPromoteValues').insert({
doc: Long.fromNumber(10)
, int: 10
, double: 2.2222
, array: [[Long.fromNumber(10)]]
}, function(err, doc) {
test.equal(null, err);

db.collection('shouldCorrectlyHonorPromoteValues').findOne(function(err, doc) {
test.equal(null, err);

test.deepEqual(Long.fromNumber(10), doc.doc);
test.deepEqual(new Int32(10), doc.int);
test.deepEqual(new Double(2.2222), doc.double);

db.close();
test.done();
});
});
});
}
}

exports['should correctly honor promoteValues at cursor level'] = {
// Add a tag that our runner can trigger on
// in this case we are setting that node needs to be higher than 0.10.X to run
metadata: { requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] } },

// The actual test we wish to run
test: function(configuration, test) {
var Long = configuration.require.Long,
Int32 = configuration.require.Int32,
Double = configuration.require.Double,
MongoClient = configuration.require.MongoClient;

MongoClient.connect(configuration.url(), {
promoteValues: false,
}, function(err, db) {
db.collection('shouldCorrectlyHonorPromoteValues').insert({
doc: Long.fromNumber(10)
, int: 10
, double: 2.2222
, array: [[Long.fromNumber(10)]]
}, function(err, doc) {
test.equal(null, err);

db.collection('shouldCorrectlyHonorPromoteValues').find().next(function(err, doc) {
test.equal(null, err);

test.deepEqual(Long.fromNumber(10), doc.doc);
test.deepEqual(new Int32(10), doc.int);
test.deepEqual(new Double(2.2222), doc.double);

db.close();
test.done();
});
});
});
}
}
1 change: 1 addition & 0 deletions test/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ var testFiles = [
, '/test/functional/find_and_modify_tests.js'
, '/test/functional/hang_tests.js',
, '/test/functional/disconnect_handler_tests.js',
, '/test/functional/promote_values_tests.js',

// Replicaset tests
, '/test/functional/replset_read_preference_tests.js'
Expand Down

0 comments on commit 8b397f1

Please sign in to comment.