syncIndexes() should handle schema-level collation differences
See original GitHub issueI ran into an issue today where sorting on an indexed field was hitting the 32MB MongoDB memory issue.
It turns out that defining new Schema({ ..., full_name: { index: true, type: String } }, { collation: { locale: 'en' } });
doesn’t automatically set the collation option on the index for full_name
.
I’ve fixed it manually by:
> db.users.dropIndex({ full_name: 1 });
{ "nIndexesWas" : 38, "ok" : 1 }
> db.users.ensureIndex({ full_name: 1 }, { collation: { locale: 'en' } });
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 37,
"numIndexesAfter" : 38,
"ok" : 1
}
However if I migrate this to another server, Mongoose doesn’t set this by default, so I’d have to re-run the above script.
Is there a way to set this as a global default if collation: ...
option is passed to the schema for indices?
Issue Analytics
- State:
- Created 5 years ago
- Reactions:3
- Comments:5 (2 by maintainers)
Top Results From Across the Web
What's New in Mongoose 5.2.0: syncIndexes()
index({ name: 1 });. In Mongoose, you declare indexes in your schemas. When you compile a model from your schema, Mongoose will build...
Read more >Mongoose v6.8.2: API docs
syncIndexes() ; Mongoose.prototype.trusted(); Mongoose.prototype.version ... Used for declaring paths in your schema that should be MongoDB ObjectIds.
Read more >types/mongoose/index.d.ts
278, * Tests whether Mongoose can cast a value to an ObjectId ... 2056, * There are also minor differences in how `countDocuments()`...
Read more >How MongoDB Collation Settings Affect Query Results and ...
To allow for the handling of large datasets, you can set the allowDiskUse option in the aggregate() method. The allowDiskUse option enables most ......
Read more >mongoose | Yarn - Package Manager
... fix: skip findOneAndReplace() validation if runValidators = false #11559; fix(model): correctly handle schema-level collations in syncIndexes() #7621 ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
For anyone else reading this, here is the workaround (remove
index: true
in your schema first):I took a closer look and it looks like #9912 does fix this:
Prints:
However, keep in mind that adding a
collation
option to your schema will not affect existing indexes, unless you callsyncIndexes()
. Mongoose doesn’t modify existing indexes by default. There is an issue withsyncIndexes()
with different collations that we will fix for 6.3.