Skip to content

Commit dca1d70

Browse files
committedMar 31, 2021
Merge branch 'master' of github.com:Automattic/mongoose
2 parents 2648088 + 966770f commit dca1d70

File tree

4 files changed

+84
-3
lines changed

4 files changed

+84
-3
lines changed
 

‎lib/model.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -1575,7 +1575,6 @@ function _ensureIndexes(model, options, callback) {
15751575
let indexError;
15761576

15771577
options = options || {};
1578-
15791578
const done = function(err) {
15801579
if (err && !model.$caught) {
15811580
model.emit('error', err);
@@ -1634,8 +1633,13 @@ function _ensureIndexes(model, options, callback) {
16341633

16351634
const indexFields = utils.clone(index[0]);
16361635
const indexOptions = utils.clone(index[1]);
1636+
let isTextIndex = false;
1637+
for (const key of Object.keys(indexFields)) {
1638+
if (indexFields[key] === 'text') {
1639+
isTextIndex = true;
1640+
}
1641+
}
16371642
delete indexOptions._autoIndex;
1638-
16391643
_decorateDiscriminatorIndexOptions(model, indexOptions);
16401644
if ('safe' in options) {
16411645
_handleSafe(options);
@@ -1654,7 +1658,8 @@ function _ensureIndexes(model, options, callback) {
16541658
indexOptions.background = options.background;
16551659
}
16561660
if (model.schema.options.hasOwnProperty('collation') &&
1657-
!indexOptions.hasOwnProperty('collation')) {
1661+
!indexOptions.hasOwnProperty('collation') &&
1662+
!isTextIndex) {
16581663
indexOptions.collation = model.schema.options.collation;
16591664
}
16601665

‎lib/query.js

+47
Original file line numberDiff line numberDiff line change
@@ -1083,6 +1083,53 @@ Query.prototype.session = function session(v) {
10831083
return this;
10841084
};
10851085

1086+
/**
1087+
* Sets the 3 write concern parameters for this query:
1088+
*
1089+
* - `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.
1090+
* - `j`: Boolean, set to `true` to request acknowledgement that this operation has been persisted to MongoDB's on-disk journal.
1091+
* - `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.
1092+
*
1093+
* This option is only valid for operations that write to the database:
1094+
*
1095+
* - `deleteOne()`
1096+
* - `deleteMany()`
1097+
* - `findOneAndDelete()`
1098+
* - `findOneAndReplace()`
1099+
* - `findOneAndUpdate()`
1100+
* - `remove()`
1101+
* - `update()`
1102+
* - `updateOne()`
1103+
* - `updateMany()`
1104+
*
1105+
* Defaults to the schema's [`writeConcern` option](/docs/guide.html#writeConcern)
1106+
*
1107+
* ####Example:
1108+
*
1109+
* // The 'majority' option means the `deleteOne()` promise won't resolve
1110+
* // until the `deleteOne()` has propagated to the majority of the replica set
1111+
* await mongoose.model('Person').
1112+
* deleteOne({ name: 'Ned Stark' }).
1113+
* writeConcern({ w: 'majority' });
1114+
*
1115+
* @method writeConcern
1116+
* @memberOf Query
1117+
* @instance
1118+
* @param {Object} writeConcern the write concern value to set
1119+
* @see mongodb https://mongodb.github.io/node-mongodb-native/3.1/api/global.html#WriteConcern
1120+
* @return {Query} this
1121+
* @api public
1122+
*/
1123+
1124+
Query.prototype.writeConcern = function writeConcern(val) {
1125+
if (val == null) {
1126+
delete this.options.writeConcern;
1127+
return this;
1128+
}
1129+
this.options.writeConcern = val;
1130+
return this;
1131+
};
1132+
10861133
/**
10871134
* Sets the specified number of `mongod` servers, or tag set of `mongod` servers,
10881135
* that must acknowledge this write before this write is considered successful.

‎test/model.indexes.test.js

+17
Original file line numberDiff line numberDiff line change
@@ -666,5 +666,22 @@ describe('model', function() {
666666
]);
667667
});
668668
});
669+
it('should prevent collation on text indexes (gh-10044)', function() {
670+
return co(function*() {
671+
const userSchema = new mongoose.Schema({ username: String }, {
672+
collation: {
673+
locale: 'en',
674+
strength: 2
675+
}
676+
});
677+
userSchema.index({ username: 'text' }, { unique: true });
678+
const User = db.model('User', userSchema, 'User');
679+
680+
yield User.init();
681+
const indexes = yield User.listIndexes();
682+
assert.ok(!indexes[1].collation);
683+
yield User.collection.drop();
684+
});
685+
});
669686
});
670687
});

‎test/query.test.js

+12
Original file line numberDiff line numberDiff line change
@@ -3776,4 +3776,16 @@ describe('Query', function() {
37763776

37773777
assert.deepEqual(q._fields, { doesntpopulate: 0, populatescorrectly: 0 });
37783778
});
3779+
3780+
it('sets `writeConcern` option correctly (gh-10009)', function() {
3781+
const testSchema = new mongoose.Schema({
3782+
name: String
3783+
});
3784+
const Test = db.model('Test', testSchema);
3785+
3786+
const q = Test.find();
3787+
q.writeConcern({ w: 'majority', wtimeout: 1000 });
3788+
3789+
assert.deepEqual(q.options.writeConcern, { w: 'majority', wtimeout: 1000 });
3790+
});
37793791
});

0 commit comments

Comments
 (0)
Please sign in to comment.