diff --git a/lib/model.js b/lib/model.js index 2b5fbb3b5f2..d5d66d3aed8 100644 --- a/lib/model.js +++ b/lib/model.js @@ -1575,7 +1575,6 @@ function _ensureIndexes(model, options, callback) { let indexError; options = options || {}; - const done = function(err) { if (err && !model.$caught) { model.emit('error', err); @@ -1634,8 +1633,13 @@ function _ensureIndexes(model, options, callback) { const indexFields = utils.clone(index[0]); const indexOptions = utils.clone(index[1]); + let isTextIndex = false; + for (const key of Object.keys(indexFields)) { + if (indexFields[key] === 'text') { + isTextIndex = true; + } + } delete indexOptions._autoIndex; - _decorateDiscriminatorIndexOptions(model, indexOptions); if ('safe' in options) { _handleSafe(options); @@ -1654,7 +1658,8 @@ function _ensureIndexes(model, options, callback) { indexOptions.background = options.background; } if (model.schema.options.hasOwnProperty('collation') && - !indexOptions.hasOwnProperty('collation')) { + !indexOptions.hasOwnProperty('collation') && + !isTextIndex) { indexOptions.collation = model.schema.options.collation; } diff --git a/lib/query.js b/lib/query.js index 86e2e627a22..8aa1bfeba5e 100644 --- a/lib/query.js +++ b/lib/query.js @@ -1083,6 +1083,53 @@ Query.prototype.session = function session(v) { return this; }; +/** + * Sets the 3 write concern parameters for this query: + * + * - `w`: Sets the specified number of `mongod` servers, or tag set of `mongod` servers, that must acknowledge this write before this write is considered successful. + * - `j`: Boolean, set to `true` to request acknowledgement that this operation has been persisted to MongoDB's on-disk journal. + * - `wtimeout`: If [`w > 1`](/docs/api.html#query_Query-w), the maximum amount of time to wait for this write to propagate through the replica set before this operation fails. The default is `0`, which means no timeout. + * + * This option is only valid for operations that write to the database: + * + * - `deleteOne()` + * - `deleteMany()` + * - `findOneAndDelete()` + * - `findOneAndReplace()` + * - `findOneAndUpdate()` + * - `remove()` + * - `update()` + * - `updateOne()` + * - `updateMany()` + * + * Defaults to the schema's [`writeConcern` option](/docs/guide.html#writeConcern) + * + * ####Example: + * + * // The 'majority' option means the `deleteOne()` promise won't resolve + * // until the `deleteOne()` has propagated to the majority of the replica set + * await mongoose.model('Person'). + * deleteOne({ name: 'Ned Stark' }). + * writeConcern({ w: 'majority' }); + * + * @method writeConcern + * @memberOf Query + * @instance + * @param {Object} writeConcern the write concern value to set + * @see mongodb https://mongodb.github.io/node-mongodb-native/3.1/api/global.html#WriteConcern + * @return {Query} this + * @api public + */ + +Query.prototype.writeConcern = function writeConcern(val) { + if (val == null) { + delete this.options.writeConcern; + return this; + } + this.options.writeConcern = val; + return this; +}; + /** * Sets the specified number of `mongod` servers, or tag set of `mongod` servers, * that must acknowledge this write before this write is considered successful. diff --git a/test/model.indexes.test.js b/test/model.indexes.test.js index d6d262bd91c..a0c339cdbae 100644 --- a/test/model.indexes.test.js +++ b/test/model.indexes.test.js @@ -666,5 +666,22 @@ describe('model', function() { ]); }); }); + it('should prevent collation on text indexes (gh-10044)', function() { + return co(function*() { + const userSchema = new mongoose.Schema({ username: String }, { + collation: { + locale: 'en', + strength: 2 + } + }); + userSchema.index({ username: 'text' }, { unique: true }); + const User = db.model('User', userSchema, 'User'); + + yield User.init(); + const indexes = yield User.listIndexes(); + assert.ok(!indexes[1].collation); + yield User.collection.drop(); + }); + }); }); }); diff --git a/test/query.test.js b/test/query.test.js index e8cb49c9d17..ae121ad2b6d 100644 --- a/test/query.test.js +++ b/test/query.test.js @@ -3776,4 +3776,16 @@ describe('Query', function() { assert.deepEqual(q._fields, { doesntpopulate: 0, populatescorrectly: 0 }); }); + + it('sets `writeConcern` option correctly (gh-10009)', function() { + const testSchema = new mongoose.Schema({ + name: String + }); + const Test = db.model('Test', testSchema); + + const q = Test.find(); + q.writeConcern({ w: 'majority', wtimeout: 1000 }); + + assert.deepEqual(q.options.writeConcern, { w: 'majority', wtimeout: 1000 }); + }); });