Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add query support for $collation (Mongo 3.4+) #70

Open
ericyhwang opened this issue Oct 4, 2018 · 0 comments
Open

Add query support for $collation (Mongo 3.4+) #70

ericyhwang opened this issue Oct 4, 2018 · 0 comments

Comments

@ericyhwang
Copy link
Contributor

Background

Mongo 3.4 added support for collation in string comparisons, for finding and sorting. This enables, among other things, sorting while ignoring case and accent differences based on a provided locale.

With a plain Mongo JS client, a case-insensitive sort would use cursor.collation:

db.my_collection.find(query)
  .sort({myStringField: 1})
  .limit(1)
  .collation({locale: 'en_US', strength: 1 /*or 2*/});

With a larger result set, a matching case-insensitive index would also be desirable for performance.

sharedb-mongo doesn't support collations yet in queries. Yet! That's what this is about.

In the meantime, there are some workarounds for doing case-insensitive sorting.

Implementation

Thankfully, it's straightforward to add support in sharedb-mongo for collation.

The usage that's equivalent to the Mongo JS driver example above would look like this:

connection.createSubscribeQuery('my_collection', {
  $sort: { myStringField: 1 },
  $collation: {locale: 'en_US', strength: 1 /*or 2*/},
});

sharedb-mongo has a map from dollar-prefixed query properties to cursor methods, and supporting collation is as easy as adding a $collation entry.

There are a couple additional pieces:

  • Tests: The one slightly tricky part with the tests is that Share runs its tests against Mongo server 2.6, 3.6, and 4.0, and collation won't work when running against 2.6. I believe it should be possible to have the test code check the MONGODB_VERSION env variable set in Travis and conditionally run specific test cases that way.
  • Error handling: Since collation was added in Mongo 3.4, any server versions before that will not handle collation.
    • Whoever implements this should check behavior against older servers and make sure something sensible happens error-wise.
    • Things are mostly fine client-wise. sharedb-mongo depends on mongodb ^2.1.2. That will result in installing [email protected], which does support cursor.collation. Users can pass in a pre-created Mongo client, though, which may be an issue.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant