Skip to content

Commit ccc40b8

Browse files
committed
Merge branch '3.5' into 3.6
2 parents 91223e4 + e43d39a commit ccc40b8

19 files changed

+175
-91
lines changed

lib/collection.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ const DEPRECATED_FIND_OPTIONS = ['maxScan', 'fields', 'snapshot', 'oplogReplay']
310310
* @param {(ReadPreference|string)} [options.readPreference] The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
311311
* @param {boolean} [options.partial=false] Specify if the cursor should return partial results when querying against a sharded system
312312
* @param {number} [options.maxTimeMS] Number of milliseconds to wait before aborting the query.
313-
* @param {number} [options.maxAwaitTimeMS] The maximum amount of time for the server to wait on new documents to satisfy a tailable cursor query. Requires `taiable` and `awaitData` to be true
313+
* @param {number} [options.maxAwaitTimeMS] The maximum amount of time for the server to wait on new documents to satisfy a tailable cursor query. Requires `tailable` and `awaitData` to be true
314314
* @param {boolean} [options.noCursorTimeout] The server normally times out idle cursors after an inactivity period (10 minutes) to prevent excess memory use. Set this option to prevent that.
315315
* @param {object} [options.collation] Specify collation (MongoDB 3.4 or higher) settings for update operation (see 3.4 documentation for available fields).
316316
* @param {boolean} [options.allowDiskUse] Enables writing to temporary files on the server.

lib/core/sdam/server.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -473,11 +473,13 @@ function markServerUnknown(server, error) {
473473
}
474474

475475
function makeOperationHandler(server, options, callback) {
476+
const session = options && options.session;
477+
476478
return function handleOperationResult(err, result) {
477479
if (err) {
478480
if (err instanceof MongoNetworkError) {
479-
if (options && options.session) {
480-
options.session.serverSession.isDirty = true;
481+
if (session && !session.hasEnded) {
482+
session.serverSession.isDirty = true;
481483
}
482484

483485
if (supportsRetryableWrites(server)) {

lib/core/sessions.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,14 @@ class ClientSession extends EventEmitter {
123123
this.abortTransaction(); // pass in callback?
124124
}
125125

126-
// mark the session as ended, and emit a signal
127-
this.hasEnded = true;
128-
this.emit('ended', this);
129-
130126
// release the server session back to the pool
131127
this.sessionPool.release(this.serverSession);
132128
this.serverSession = null;
133129

130+
// mark the session as ended, and emit a signal
131+
this.hasEnded = true;
132+
this.emit('ended', this);
133+
134134
// spec indicates that we should ignore all errors for `endSessions`
135135
if (typeof callback === 'function') callback(null, null);
136136
}

lib/core/uri_parser.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,8 @@ function applyConnectionStringOption(obj, key, value, options) {
293293
}
294294
}
295295

296-
if (key === 'readpreferencetags' && Array.isArray(value)) {
297-
value = splitArrayOfMultipleReadPreferenceTags(value);
296+
if (key === 'readpreferencetags') {
297+
value = Array.isArray(value) ? splitArrayOfMultipleReadPreferenceTags(value) : [value];
298298
}
299299

300300
// set the actual value

lib/operations/create_collection.js

Lines changed: 52 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ const Aspect = require('./operation').Aspect;
44
const defineAspects = require('./operation').defineAspects;
55
const CommandOperation = require('./command');
66
const applyWriteConcern = require('../utils').applyWriteConcern;
7+
const handleCallback = require('../utils').handleCallback;
78
const loadCollection = require('../dynamic_loaders').loadCollection;
89
const MongoError = require('../core').MongoError;
910
const ReadPreference = require('../core').ReadPreference;
1011

11-
const ILLEGAL_COMMAND_FIELDS = new Set([
12+
// Filter out any write concern options
13+
const illegalCommandFields = [
1214
'w',
1315
'wtimeout',
1416
'j',
@@ -22,24 +24,27 @@ const ILLEGAL_COMMAND_FIELDS = new Set([
2224
'session',
2325
'readConcern',
2426
'writeConcern'
25-
]);
27+
];
2628

2729
class CreateCollectionOperation extends CommandOperation {
2830
constructor(db, name, options) {
2931
super(db, options);
32+
3033
this.name = name;
3134
}
3235

3336
_buildCommand() {
3437
const name = this.name;
3538
const options = this.options;
3639

40+
// Create collection command
3741
const cmd = { create: name };
42+
// Add all optional parameters
3843
for (let n in options) {
3944
if (
4045
options[n] != null &&
4146
typeof options[n] !== 'function' &&
42-
!ILLEGAL_COMMAND_FIELDS.has(n)
47+
illegalCommandFields.indexOf(n) === -1
4348
) {
4449
cmd[n] = options[n];
4550
}
@@ -52,51 +57,61 @@ class CreateCollectionOperation extends CommandOperation {
5257
const db = this.db;
5358
const name = this.name;
5459
const options = this.options;
55-
const Collection = loadCollection();
56-
57-
let listCollectionOptions = Object.assign({ nameOnly: true, strict: false }, options);
58-
listCollectionOptions = applyWriteConcern(listCollectionOptions, { db }, listCollectionOptions);
5960

60-
function done(err) {
61-
if (err) {
62-
return callback(err);
63-
}
61+
let Collection = loadCollection();
6462

65-
try {
66-
callback(
67-
null,
68-
new Collection(db, db.s.topology, db.databaseName, name, db.s.pkFactory, options)
69-
);
70-
} catch (err) {
71-
callback(err);
72-
}
63+
// Did the user destroy the topology
64+
if (db.serverConfig && db.serverConfig.isDestroyed()) {
65+
return callback(new MongoError('topology was destroyed'));
7366
}
7467

75-
const strictMode = listCollectionOptions.strict;
76-
if (strictMode) {
77-
db.listCollections({ name }, listCollectionOptions)
78-
.setReadPreference(ReadPreference.PRIMARY)
79-
.toArray((err, collections) => {
80-
if (err) {
81-
return callback(err);
82-
}
68+
let listCollectionOptions = Object.assign({}, options, { nameOnly: true });
69+
listCollectionOptions = applyWriteConcern(listCollectionOptions, { db }, listCollectionOptions);
8370

84-
if (collections.length > 0) {
85-
return callback(
86-
new MongoError(`Collection ${name} already exists. Currently in strict mode.`)
71+
// Check if we have the name
72+
db.listCollections({ name }, listCollectionOptions)
73+
.setReadPreference(ReadPreference.PRIMARY)
74+
.toArray((err, collections) => {
75+
if (err != null) return handleCallback(callback, err, null);
76+
if (collections.length > 0 && listCollectionOptions.strict) {
77+
return handleCallback(
78+
callback,
79+
MongoError.create({
80+
message: `Collection ${name} already exists. Currently in strict mode.`,
81+
driver: true
82+
}),
83+
null
84+
);
85+
} else if (collections.length > 0) {
86+
try {
87+
return handleCallback(
88+
callback,
89+
null,
90+
new Collection(db, db.s.topology, db.databaseName, name, db.s.pkFactory, options)
8791
);
92+
} catch (err) {
93+
return handleCallback(callback, err);
8894
}
95+
}
8996

90-
super.execute(done);
91-
});
92-
93-
return;
94-
}
97+
// Execute command
98+
super.execute(err => {
99+
if (err) return handleCallback(callback, err);
95100

96-
// otherwise just execute the command
97-
super.execute(done);
101+
try {
102+
return handleCallback(
103+
callback,
104+
null,
105+
new Collection(db, db.s.topology, db.databaseName, name, db.s.pkFactory, options)
106+
);
107+
} catch (err) {
108+
return handleCallback(callback, err);
109+
}
110+
});
111+
});
98112
}
99113
}
100114

101115
defineAspects(CreateCollectionOperation, Aspect.WRITE_OPERATION);
116+
102117
module.exports = CreateCollectionOperation;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
},
2626
"dependencies": {
2727
"bl": "^2.2.0",
28-
"bson": "^1.1.1",
28+
"bson": "^1.1.4",
2929
"denque": "^1.4.1",
3030
"require_optional": "^1.0.1",
3131
"safe-buffer": "^5.1.2"

test/examples/change_streams.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,8 @@ describe('examples(change-stream):', function() {
1616
client = await this.configuration.newClient().connect();
1717
db = client.db(this.configuration.db);
1818

19-
// ensure database exists, we need this for 3.6
20-
await db.collection('inventory').insertOne({});
21-
22-
// now clear the collection
23-
await db.collection('inventory').deleteMany();
19+
await db.createCollection('inventory');
20+
await db.collection('inventory').deleteMany({});
2421
});
2522

2623
afterEach(async function() {

test/functional/collection.test.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ describe('Collection', function() {
1010
let configuration;
1111
before(function() {
1212
configuration = this.configuration;
13-
return setupDatabase(configuration, ['listCollectionsDb', 'listCollectionsDb2', 'test_db']);
13+
return setupDatabase(configuration);
1414
});
1515

1616
describe('standard collection tests', function() {
@@ -208,7 +208,12 @@ describe('Collection', function() {
208208
'Collection test_strict_create_collection already exists. Currently in strict mode.'
209209
);
210210

211-
done();
211+
// Switch out of strict mode and try to re-create collection
212+
db.createCollection('test_strict_create_collection', { strict: false }, err => {
213+
expect(err).to.not.exist;
214+
// Let's close the db
215+
done();
216+
});
212217
});
213218
});
214219
});
@@ -744,7 +749,7 @@ describe('Collection', function() {
744749
expect(coll).to.exist;
745750

746751
db.createCollection('shouldFailDueToExistingCollection', { strict: true }, err => {
747-
expect(err).to.exist;
752+
expect(err).to.be.an.instanceof(Error);
748753
expect(err.message).to.equal(
749754
'Collection shouldFailDueToExistingCollection already exists. Currently in strict mode.'
750755
);

test/functional/cursor.test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2549,10 +2549,7 @@ describe('Cursor', function() {
25492549

25502550
var db = client.db(configuration.db);
25512551
var options = { capped: true, size: 8 };
2552-
db.createCollection('should_await_data_retry_tailable_cursor', options, function(
2553-
err,
2554-
collection
2555-
) {
2552+
db.createCollection('should_await_data', options, function(err, collection) {
25562553
test.equal(null, err);
25572554

25582555
collection.insert({ a: 1 }, configuration.writeConcernMax(), function(err) {
@@ -4045,7 +4042,10 @@ describe('Cursor', function() {
40454042
test.equal(null, err);
40464043

40474044
var db = client.db(configuration.db);
4048-
db.createCollection('negative_batch_size_and_limit_set', function(err, collection) {
4045+
db.createCollection('Should_correctly_execute_count_on_cursor_1_', function(
4046+
err,
4047+
collection
4048+
) {
40494049
test.equal(null, err);
40504050

40514051
// insert all docs

test/functional/find.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1379,7 +1379,7 @@ describe('Find', function() {
13791379
var client = configuration.newClient(configuration.writeConcernMax(), { poolSize: 1 });
13801380
client.connect(function(err, client) {
13811381
var db = client.db(configuration.db);
1382-
db.createCollection('execute_find_and_modify', function(err, collection) {
1382+
db.createCollection('shouldCorrectlyExecuteFindAndModify', function(err, collection) {
13831383
var self = { _id: new ObjectID() };
13841384
var _uuid = 'sddffdss';
13851385

@@ -1601,7 +1601,7 @@ describe('Find', function() {
16011601
transactions: transactions
16021602
};
16031603

1604-
db.createCollection('find_and_modify_generate_correct_bson', function(err, collection) {
1604+
db.createCollection('shouldCorrectlyExecuteFindAndModify', function(err, collection) {
16051605
test.equal(null, err);
16061606

16071607
collection.insert(wrapingObject, configuration.writeConcernMax(), function(err, r) {

0 commit comments

Comments
 (0)