Skip to content

Commit

Permalink
Merge branch 'master' of github.com:Automattic/mongoose
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Mar 31, 2021
2 parents 2648088 + 966770f commit dca1d70
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/model.js
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}

Expand Down
47 changes: 47 additions & 0 deletions lib/query.js
Expand Up @@ -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.
Expand Down
17 changes: 17 additions & 0 deletions test/model.indexes.test.js
Expand Up @@ -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();
});
});
});
});
12 changes: 12 additions & 0 deletions test/query.test.js
Expand Up @@ -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 });
});
});

0 comments on commit dca1d70

Please sign in to comment.