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 case insensitive indexes support #417

Merged
merged 1 commit into from
Oct 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ of type `GeoPoint`. This allows for indexed ```near``` queries. Default is `fal
- It will try to establish the connection automatically once users hit the endpoint. If the mongodb server is offline, the app will start, however, the endpoints will not work.
- **disableDefaultSort**: Set to `true` to disable the default sorting
behavior on `id` column, this will help performance using indexed columns available in mongodb.
- **collation**: Specify language-specific rules for string comparison, such as rules for lettercase and accent marks. See [`MongdoDB documentation`](https://docs.mongodb.com/manual/reference/collation/) for details. It can also be used to create [`case insensitive indexes`](https://docs.mongodb.com/manual/core/index-case-insensitive/).

### Setting the url property in datasource.json

Expand Down
5 changes: 5 additions & 0 deletions lib/mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -1315,6 +1315,11 @@ MongoDB.prototype.all = function all(model, filter, options, callback) {
return callback(err);
}

var collation = options && options.collation;
if (collation) {
cursor.collation(collation);
}

// don't apply sorting if dealing with a geo query
if (!hasNearFilter(filter.where)) {
var order = self.buildSort(model, filter.order, options);
Expand Down
44 changes: 43 additions & 1 deletion test/mongodb.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,13 @@ describe('mongodb connector', function() {
Category = db.define('Category', {
title: {type: String, length: 255, index: true},
posts: {type: [db.ObjectID], index: true},
}, {
indexes: {
'title_case_insensitive': {
keys: {title: 1},
options: {collation: {locale: 'en', strength: 1}},
},
},
});

PostWithStringIdAndRenamedColumns = db.define(
Expand Down Expand Up @@ -314,7 +321,9 @@ describe('mongodb connector', function() {
PostWithNumberUnderscoreId.destroyAll(function() {
PostWithStringId.destroyAll(function() {
PostWithDisableDefaultSort.destroyAll(function() {
done();
Category.destroyAll(function() {
done();
});
});
});
});
Expand Down Expand Up @@ -508,6 +517,23 @@ describe('mongodb connector', function() {
});
});

it('should create case insensitive indexes', function(done) {
db.automigrate('Category', function() {
db.connector.db.collection('Category').indexes(function(err, result) {
if (err) return done(err);
var indexes = [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's check for err object here first; if (err) done(err);

{name: '_id_', key: {_id: 1}},
{name: 'title_1', key: {title: 1}},
{name: 'title_case_insensitive', key: {title: 1}, collation: {locale: 'en', strength: 1}},
{name: 'posts_1', key: {posts: 1}},
];

result.should.containDeep(indexes);
done();
});
});
});

it('should have created models with correct _id types', function(done) {
PostWithObjectId.definition.properties._id.type.should.be.equal(
db.ObjectID
Expand Down Expand Up @@ -2679,6 +2705,22 @@ describe('mongodb connector', function() {
});
});

it('should allow to find using case insensitive index', function(done) {
Category.create({title: 'My Category'}, function(err, category1) {
if (err) return done(err);
Category.create({title: 'MY CATEGORY'}, function(err, category2) {
if (err) return done(err);

Category.find({where: {title: 'my cATEGory'}}, {collation: {locale: 'en', strength: 1}},
function(err, categories) {
if (err) return done(err);
categories.should.have.length(2);
done();
});
});
});
});

it('should allow to find using like', function(done) {
Post.create({title: 'My Post', content: 'Hello'}, function(err, post) {
Post.find({where: {title: {like: 'M.+st'}}}, function(err, posts) {
Expand Down