Skip to content

Commit 393f6b0

Browse files
committed
Minor fixes to how write concern is handled with the 3.4 server
1 parent d17978a commit 393f6b0

File tree

7 files changed

+168
-81
lines changed

7 files changed

+168
-81
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ env:
1010
- MONGODB_VERSION=2.4.x
1111
- MONGODB_VERSION=2.6.x
1212
- MONGODB_VERSION=3.0.x
13-
- MONGODB_VERSION=3.2.0
13+
- MONGODB_VERSION=3.2.x
14+
- MONGODB_VERSION=3.3.x

lib/bulk/common.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,22 @@ var REMOVE = 3
1818

1919
// Get write concern
2020
var writeConcern = function(target, col, options) {
21-
if(options.w != null || options.j != null || options.fsync != null) {
22-
target.writeConcern = options;
23-
} else if(col.writeConcern.w != null || col.writeConcern.j != null || col.writeConcern.fsync != null) {
24-
target.writeConcern = col.writeConcern;
25-
}
26-
27-
return target
21+
var writeConcern = {};
22+
23+
// Collection level write concern
24+
if(col.writeConcern && col.writeConcern.w != null) writeConcern.w = col.writeConcern.w;
25+
if(col.writeConcern && col.writeConcern.j != null) writeConcern.j = col.writeConcern.j;
26+
if(col.writeConcern && col.writeConcern.fsync != null) writeConcern.fsync = col.writeConcern.fsync;
27+
if(col.writeConcern && col.writeConcern.wtimeout != null) writeConcern.wtimeout = col.writeConcern.wtimeout;
28+
29+
// Options level write concern
30+
if(options && options.w != null) writeConcern.w = options.w;
31+
if(options && options.wtimeout != null) writeConcern.wtimeout = options.wtimeout;
32+
if(options && options.j != null) writeConcern.j = options.j;
33+
if(options && options.fsync != null) writeConcern.fsync = options.fsync;
34+
35+
// Return write concern
36+
return writeConcern;
2837
}
2938

3039
/**

lib/collection.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2600,19 +2600,23 @@ Collection.prototype.aggregate = function(pipeline, options, callback) {
26002600
// Ignore readConcern option
26012601
var ignoreReadConcern = false;
26022602

2603+
// Build the command
2604+
var command = { aggregate : this.s.name, pipeline : pipeline};
2605+
26032606
// If out was specified
26042607
if(typeof options.out == 'string') {
26052608
pipeline.push({$out: options.out});
2609+
// Ignore read concern
26062610
ignoreReadConcern = true;
26072611
} else if(pipeline.length > 0 && pipeline[pipeline.length - 1]['$out']) {
26082612
ignoreReadConcern = true;
26092613
}
26102614

2611-
// Build the command
2612-
var command = { aggregate : this.s.name, pipeline : pipeline};
2615+
// Decorate command with writeConcern if out has been specified
2616+
if(pipeline.length > 0 && pipeline[pipeline.length - 1]['$out']) {
2617+
decorateWithWriteConcern(command, self, options);
2618+
}
26132619

2614-
// Decorate command with writeConcern if supported
2615-
decorateWithWriteConcern(command, self, options);
26162620
// Have we specified collation
26172621
decorateWithCollation(command, self, options);
26182622

lib/db.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,7 @@ define.classMethod('dropCollection', {callback: true, promise:true});
865865
Db.prototype.dropDatabase = function(options, callback) {
866866
var self = this;
867867
if(typeof options == 'function') callback = options, options = {};
868+
options = options || {};
868869
// Drop database command
869870
var cmd = {'dropDatabase':1};
870871

test/functional/collations_tests.js

Lines changed: 138 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,74 +1033,75 @@ exports['Fail command due to no support for collation'] = {
10331033
}
10341034
}
10351035

1036-
// exports['Successfully pass through collation to bulkWrite command'] = {
1037-
// metadata: { requires: { generators: true, topology: "single" } },
1038-
//
1039-
// test: function(configuration, test) {
1040-
// var MongoClient = configuration.require.MongoClient,
1041-
// co = require('co'),
1042-
// mockupdb = require('../mock');
1043-
//
1044-
// // Contain mock server
1045-
// var singleServer = null;
1046-
// var running = true;
1047-
//
1048-
// // Default message fields
1049-
// var defaultFields = {
1050-
// "ismaster" : true, "maxBsonObjectSize" : 16777216,
1051-
// "maxMessageSizeBytes" : 48000000, "maxWriteBatchSize" : 1000,
1052-
// "localTime" : new Date(), "maxWireVersion" : 5, "minWireVersion" : 0, "ok" : 1
1053-
// }
1054-
//
1055-
// // Primary server states
1056-
// var primary = [extend(defaultFields, {})];
1057-
//
1058-
// // Boot the mock
1059-
// co(function*() {
1060-
// singleServer = yield mockupdb.createServer(32000, 'localhost');
1061-
//
1062-
// // Primary state machine
1063-
// co(function*() {
1064-
// while(running) {
1065-
// var request = yield singleServer.receive();
1066-
// var doc = request.document;
1067-
// console.log("========================== cmd")
1068-
// console.dir(doc)
1069-
//
1070-
// if(doc.ismaster) {
1071-
// request.reply(primary[0]);
1072-
// } else if(doc.update) {
1073-
// commandResult = doc;
1074-
// request.reply({ok:1});
1075-
// }
1076-
// }
1077-
// }).catch(function(err) {
1078-
// console.log(err.stack);
1079-
// });
1080-
//
1081-
// var commandResult = null;
1082-
//
1083-
// // Connect to the mocks
1084-
// MongoClient.connect('mongodb://localhost:32000/test', {collation: { caseLevel: true }}, function(err, db) {
1085-
// test.equal(null, err);
1086-
//
1087-
// console.log("!!!!!!!!!!!!!!!!!!! 0")
1088-
// db.collection('test').bulkWrite([
1089-
// { updateOne: { q: {a:2}, u: {$set: {a:2}}, upsert:true } }
1090-
// , { deleteOne: { q: {c:1} } }
1091-
// ], {ordered:true}, function(err, r) {
1092-
// console.log("!!!!!!!!!!!!!!!!!!! 1")
1093-
// console.dir(err)
1094-
// singleServer.destroy();
1095-
// running = false;
1096-
//
1097-
// db.close();
1098-
// test.done();
1099-
// });
1100-
// });
1101-
// });
1102-
// }
1103-
// }
1036+
exports['Successfully pass through collation to bulkWrite command'] = {
1037+
metadata: { requires: { generators: true, topology: "single" } },
1038+
1039+
test: function(configuration, test) {
1040+
var MongoClient = configuration.require.MongoClient,
1041+
co = require('co'),
1042+
mockupdb = require('../mock');
1043+
1044+
// Contain mock server
1045+
var singleServer = null;
1046+
var running = true;
1047+
1048+
// Default message fields
1049+
var defaultFields = {
1050+
"ismaster" : true, "maxBsonObjectSize" : 16777216,
1051+
"maxMessageSizeBytes" : 48000000, "maxWriteBatchSize" : 1000,
1052+
"localTime" : new Date(), "maxWireVersion" : 5, "minWireVersion" : 0, "ok" : 1
1053+
}
1054+
1055+
// Primary server states
1056+
var primary = [extend(defaultFields, {})];
1057+
1058+
// Boot the mock
1059+
co(function*() {
1060+
singleServer = yield mockupdb.createServer(32000, 'localhost');
1061+
1062+
// Primary state machine
1063+
co(function*() {
1064+
while(running) {
1065+
var request = yield singleServer.receive();
1066+
var doc = request.document;
1067+
// console.log("========================== cmd")
1068+
// console.dir(doc)
1069+
1070+
if(doc.ismaster) {
1071+
request.reply(primary[0]);
1072+
} else if(doc.update) {
1073+
commandResult = doc;
1074+
request.reply({ok:1});
1075+
} else if(doc.delete) {
1076+
request.reply({ok:1});
1077+
}
1078+
}
1079+
}).catch(function(err) {
1080+
console.log(err.stack);
1081+
});
1082+
1083+
var commandResult = null;
1084+
1085+
// Connect to the mocks
1086+
MongoClient.connect('mongodb://localhost:32000/test', function(err, db) {
1087+
test.equal(null, err);
1088+
1089+
db.collection('test').bulkWrite([
1090+
{ updateOne: { q: {a:2}, u: {$set: {a:2}}, upsert:true, collation: { caseLevel: true } } }
1091+
, { deleteOne: { q: {c:1} } }
1092+
], {ordered:true}, function(err, r) {
1093+
test.ok(commandResult);
1094+
test.deepEqual({ caseLevel: true }, commandResult.updates[0].collation);
1095+
singleServer.destroy();
1096+
running = false;
1097+
1098+
db.close();
1099+
test.done();
1100+
});
1101+
});
1102+
});
1103+
}
1104+
}
11041105

11051106
exports['Successfully fail bulkWrite due to unsupported collation'] = {
11061107
metadata: { requires: { generators: true, topology: "single" } },
@@ -1480,3 +1481,72 @@ exports['Fail to create indexs with collation due to no capabilities'] = {
14801481
});
14811482
}
14821483
}
1484+
1485+
/******************************************************************************
1486+
.___ __ __ .__
1487+
| | _____/ |_ ____ ________________ _/ |_|__| ____ ____
1488+
| |/ \ __\/ __ \ / ___\_ __ \__ \\ __\ |/ _ \ / \
1489+
| | | \ | \ ___// /_/ > | \// __ \| | | ( <_> ) | \
1490+
|___|___| /__| \___ >___ /|__| (____ /__| |__|\____/|___| /
1491+
\/ \/_____/ \/ \/
1492+
******************************************************************************/
1493+
exports['Should correctly create index with collation'] = {
1494+
metadata: { requires: { topology: "single", mongodb: ">=3.3.12" } },
1495+
1496+
test: function(configuration, test) {
1497+
var MongoClient = configuration.require.MongoClient,
1498+
co = require('co');
1499+
1500+
// Connect to the mocks
1501+
MongoClient.connect(configuration.url(), function(err, db) {
1502+
test.equal(null, err);
1503+
1504+
var col = db.collection('collation_test');
1505+
// Create collation index
1506+
col.createIndexes([{key: {a:1}, collation: { locale: 'nn' }, name: 'collation_test'}], function(err, r) {
1507+
test.equal(null, err);
1508+
1509+
col.listIndexes().toArray(function(err, r) {
1510+
var indexes = r.filter(function(i) {
1511+
return i.name == 'collation_test';
1512+
})
1513+
1514+
test.equal(1, indexes.length);
1515+
test.ok(indexes[0].collation);
1516+
1517+
db.close();
1518+
test.done();
1519+
});
1520+
});
1521+
});
1522+
}
1523+
}
1524+
1525+
exports['Should correctly create collection with collation'] = {
1526+
metadata: { requires: { topology: "single", mongodb: ">=3.3.12" } },
1527+
1528+
test: function(configuration, test) {
1529+
var MongoClient = configuration.require.MongoClient,
1530+
co = require('co');
1531+
1532+
// Connect to the mocks
1533+
MongoClient.connect(configuration.url(), function(err, db) {
1534+
test.equal(null, err);
1535+
1536+
// Simple findAndModify command returning the new document
1537+
db.createCollection('collation_test2', {collation: { locale: 'nn' }}, function(err, results) {
1538+
test.equal(null, err);
1539+
1540+
db.listCollections({name: 'collation_test2'}).toArray(function(err, collections) {
1541+
test.equal(null, err);
1542+
test.equal(1, collections.length);
1543+
test.equal('collation_test2', collections[0].name);
1544+
test.ok(collections[0].options.collation);
1545+
1546+
db.close();
1547+
test.done();
1548+
});
1549+
});
1550+
});
1551+
}
1552+
}

test/functional/cursor_tests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2530,7 +2530,7 @@ exports['should correctly apply hint to count command for cursor'] = {
25302530
test.equal(null, err);
25312531
test.equal(0, count);
25322532

2533-
col.find({}, {hint: "x_1"}).count(function(err, count) {
2533+
col.find({}, {hint: "i_1"}).count(function(err, count) {
25342534
test.equal(null, err);
25352535
test.equal(2, count);
25362536

test/functional/promises_db_tests.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,8 @@ exports['Should correctly drop database using Promise'] = {
255255

256256
db.close();
257257
test.done();
258+
}).catch(function(e) {
259+
console.dir(e)
258260
});
259261
});
260262
}

0 commit comments

Comments
 (0)