question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

syncIndexes() should handle schema-level collation differences

See original GitHub issue

I 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:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
niftylettucecommented, Mar 21, 2019

For anyone else reading this, here is the workaround (remove index: true in your schema first):

// fix per <https://github.com/Automattic/mongoose/issues/7621>
User.index({ full_name: 1 }, { collation: { locale: 'en' } });
0reactions
vkarpov15commented, Apr 5, 2022

I took a closer look and it looks like #9912 does fix this:

    const testSchema = new Schema(
      { name: { type: String, index: true } },
      { collation: { locale: 'en' } }
    );
    const Test = db.model('Test', testSchema);

    await Test.init();

    console.log(await Test.collection.listIndexes().toArray());

Prints:

[
  {
    v: 2,
    key: { _id: 1 },
    name: '_id_',
    collation: {
      locale: 'en',
      caseLevel: false,
      caseFirst: 'off',
      strength: 3,
      numericOrdering: false,
      alternate: 'non-ignorable',
      maxVariable: 'punct',
      normalization: false,
      backwards: false,
      version: '57.1'
    }
  },
  {
    v: 2,
    key: { name: 1 },
    name: 'name_1',
    background: true,
    collation: {
      locale: 'en',
      caseLevel: false,
      caseFirst: 'off',
      strength: 3,
      numericOrdering: false,
      alternate: 'non-ignorable',
      maxVariable: 'punct',
      normalization: false,
      backwards: false,
      version: '57.1'
    }
  }
]

However, keep in mind that adding a collation option to your schema will not affect existing indexes, unless you call syncIndexes(). Mongoose doesn’t modify existing indexes by default. There is an issue with syncIndexes() with different collations that we will fix for 6.3.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found