Skip to content

Commit dd4d9ba

Browse files
committed
Added support for correct mapreduce function on > 1.7.6 and higher
1 parent f1e0588 commit dd4d9ba

File tree

4 files changed

+33
-9
lines changed

4 files changed

+33
-9
lines changed

integration/integration_tests.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3481,7 +3481,7 @@ var all_tests = {
34813481
});
34823482
});
34833483
},
3484-
3484+
34853485
test_custom_primary_key_generator : function() {
34863486
// Custom factory (need to provide a 12 byte array);
34873487
CustomPKFactory = function() {}
@@ -3538,7 +3538,7 @@ var all_tests = {
35383538
test_map_reduce_functions_scope : function() {
35393539
client.createCollection('test_map_reduce_functions_scope', function(err, collection) {
35403540
collection.insert([{'user_id':1, 'timestamp':new Date()}, {'user_id':2, 'timestamp':new Date()}]);
3541-
3541+
35423542
var map = function(){
35433543
emit(test(this.timestamp.getYear()), 1);
35443544
}

lib/mongodb/admin.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@ var Admin = exports.Admin = function(db) {
66
this.db = db;
77
};
88

9+
Admin.prototype.serverInfo = function(callback) {
10+
var self = this;
11+
var command = {buildinfo:1};
12+
var databaseName = self.db.databaseName;
13+
self.db.databaseName = 'admin';
14+
this.db.executeDbCommand(command, function(err, doc) {
15+
self.db.databaseName = databaseName;
16+
if(err != null) return callback(err, null);
17+
return callback(null, doc.documents[0]);
18+
});
19+
}
20+
921
Admin.prototype.profilingLevel = function(callback) {
1022
var command = {profile:-1};
1123
this.db.executeDbCommand(command, function(err, doc) {

lib/mongodb/collection.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -478,9 +478,15 @@ Collection.prototype.dropIndexes = function(callback) {
478478
Collection.prototype.mapReduce = function(map, reduce, options, callback) {
479479
var args = Array.prototype.slice.call(arguments, 2);
480480
callback = args.pop();
481-
options = args.length ? args.shift() : {};
482-
483481
var self = this;
482+
// Parse version of server if available
483+
var version = this.db.version != null ? parseInt(this.db.version.replace(/\./g, '')) : 0;
484+
485+
// Set default to be inline if we are dealing with a v 1.7.6 > server
486+
if(version > 0 && version > 176) {
487+
options = args.length ? args.shift() : {out:'inline'};
488+
if(options.out == null) options.out = 'inline';
489+
}
484490

485491
if(Object.prototype.toString.call(map) === '[object Function]') map = map.toString();
486492
if(Object.prototype.toString.call(reduce) === '[object Function]') reduce = reduce.toString();

lib/mongodb/db.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ Db.prototype.open = function(callback) {
4949

5050
self.serverConfig.connection.addListener("connect", function() {
5151
// Create a callback function for a given connection
52-
var connectCallback = function(err, reply) {
52+
var connectCallback = function(err, reply) {
5353
if(err != null) {
5454
return callback(err, null);
5555
} else if(reply.documents[0].ismaster == 1) {
@@ -61,7 +61,16 @@ Db.prototype.open = function(callback) {
6161
// emit a message saying we got a master and are ready to go and change state to reflect it
6262
if(self.state == 'notConnected') {
6363
self.state = 'connected';
64-
callback(null, self);
64+
//
65+
// Call the server version function via admin to adapt to changes from 1.7.6 >
66+
self.admin(function(err, admindb) {
67+
admindb.serverInfo(function(err, doc) {
68+
if(err != null) return callback(err, null);
69+
// Store the db version
70+
self.version = doc.version;
71+
callback(null, self);
72+
});
73+
});
6574
} else {
6675
callback("connection already opened");
6776
}
@@ -78,8 +87,6 @@ Db.prototype.open = function(callback) {
7887
// Parse the data as a reply object
7988
var reply = new MongoReply(self, message);
8089
// Emit message
81-
82-
8390
self.emit(reply.responseTo.toString(), null, reply);
8491
// Remove the listener
8592
self.removeListener(reply.responseTo.toString(), self.listeners(reply.responseTo.toString())[0]);
@@ -166,7 +173,6 @@ Db.prototype.open = function(callback) {
166173
};
167174

168175
Db.prototype.close = function() {
169-
170176
this.connections.forEach(function(connection) {
171177
connection.close();
172178
});

0 commit comments

Comments
 (0)