share / share/sharedb-mongo

Add query support for $collation (Mongo 3.4+)

Open
#70 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
152
Forks
68
Avg merge
1d 5h
Merged PRs (30d)
3

Description

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 mongodb@2.2.x, which does support cursor.collation. Users can pass in a pre-created Mongo client, though, which may be an issue.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in index.js at the dollar-prefixed query-property map referenced by the issue, then review .travis.yml for the MONGODB_VERSION values and find the existing query tests. Run the relevant tests against the supported MongoDB versions; done means $collation works on compatible servers and older versions produce sensible error behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, mongodb
Domain
database
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.