Skip to content

Commit 2d2999e

Browse files
authoredAug 7, 2018
Merge pull request #437 from HugoPoi/feature/settings-disable-default-sort
Add settings disableDefaultSort for find method
2 parents b79e263 + 94e47e3 commit 2d2999e

File tree

3 files changed

+64
-7
lines changed

3 files changed

+64
-7
lines changed
 

‎README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ of type `GeoPoint`. This allows for indexed ```near``` queries. Default is `fal
6464
- Default is `false`.
6565
- If set to `true`, the database instance will not be attached to the datasource and the connection is deferred.
6666
- 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.
67-
67+
- **disableDefaultSort**: Set to `true` to disable the default sorting
68+
behavior on `id` column, this will help performance using indexed
69+
columns available in mongodb.
6870
### Setting the url property in datasource.json
6971

7072
You can set the `url` property to a connection URL in `datasources.json` to override individual connection parameters such as `host`, `user`, and `password`.

‎lib/mongodb.js

+18-4
Original file line numberDiff line numberDiff line change
@@ -936,10 +936,24 @@ MongoDB.prototype.buildWhere = function(model, where) {
936936
return query;
937937
};
938938

939-
MongoDB.prototype.buildSort = function(model, order) {
939+
MongoDB.prototype.buildSort = function(model, order, options) {
940940
var sort = {};
941941
var idName = this.idName(model);
942-
if (!order) {
942+
943+
var modelClass = this._models[model];
944+
945+
var disableDefaultSort = false;
946+
if (this.settings.hasOwnProperty('disableDefaultSort')) {
947+
disableDefaultSort = this.settings.disableDefaultSort;
948+
}
949+
if (modelClass.settings.hasOwnProperty('disableDefaultSort')) {
950+
disableDefaultSort = modelClass.settings.disableDefaultSort;
951+
}
952+
if (options && options.hasOwnProperty('disableDefaultSort')) {
953+
disableDefaultSort = options.disableDefaultSort;
954+
}
955+
956+
if (!order && !disableDefaultSort) {
943957
var idNames = this.idNames(model);
944958
if (idNames && idNames.length) {
945959
order = idNames;
@@ -966,7 +980,7 @@ MongoDB.prototype.buildSort = function(model, order) {
966980
sort[key] = 1;
967981
}
968982
}
969-
} else {
983+
} else if (!disableDefaultSort) {
970984
// order by _id by default
971985
sort = {_id: 1};
972986
}
@@ -1224,7 +1238,7 @@ MongoDB.prototype.all = function all(model, filter, options, callback) {
12241238

12251239
// don't apply sorting if dealing with a geo query
12261240
if (!hasNearFilter(filter.where)) {
1227-
var order = self.buildSort(model, filter.order);
1241+
var order = self.buildSort(model, filter.order, options);
12281242
cursor.sort(order);
12291243
}
12301244

‎test/mongodb.test.js

+43-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ var Superhero,
2626
Category,
2727
UserWithRenamedColumns,
2828
PostWithStringIdAndRenamedColumns,
29-
Employee;
29+
Employee,
30+
PostWithDisableDefaultSort;
3031

3132
describe('lazyConnect', function() {
3233
it('should skip connect phase (lazyConnect = true)', function(done) {
@@ -273,6 +274,18 @@ describe('mongodb connector', function() {
273274
}
274275
);
275276

277+
PostWithDisableDefaultSort = db.define(
278+
'PostWithDisableDefaultSort',
279+
{
280+
id: {type: String, id: true},
281+
title: {type: String, length: 255, index: true},
282+
content: {type: String},
283+
},
284+
{
285+
disableDefaultSort: true,
286+
}
287+
);
288+
276289
User.hasMany(Post);
277290
Post.belongsTo(User);
278291
});
@@ -285,7 +298,9 @@ describe('mongodb connector', function() {
285298
PostWithNumberId.destroyAll(function() {
286299
PostWithNumberUnderscoreId.destroyAll(function() {
287300
PostWithStringId.destroyAll(function() {
288-
done();
301+
PostWithDisableDefaultSort.destroyAll(function() {
302+
done();
303+
});
289304
});
290305
});
291306
});
@@ -2490,6 +2505,32 @@ describe('mongodb connector', function() {
24902505
});
24912506
});
24922507

2508+
it('find should not order by id if the order is not set for the query filter and settings.disableDefaultSort is true',
2509+
function(done) {
2510+
PostWithDisableDefaultSort.create({id: '2', title: 'c', content: 'CCC'}, function(err, post) {
2511+
PostWithDisableDefaultSort.create({id: '1', title: 'd', content: 'DDD'}, function(err, post) {
2512+
PostWithDisableDefaultSort.find({}, function(err, posts) {
2513+
should.not.exist(err);
2514+
posts.length.should.be.equal(2);
2515+
posts[0].id.should.be.equal('2');
2516+
2517+
PostWithDisableDefaultSort.find({limit: 1, offset: 0}, function(err, posts) {
2518+
should.not.exist(err);
2519+
posts.length.should.be.equal(1);
2520+
posts[0].id.should.be.equal('2');
2521+
2522+
PostWithDisableDefaultSort.find({limit: 1, offset: 1}, function(err, posts) {
2523+
should.not.exist(err);
2524+
posts.length.should.be.equal(1);
2525+
posts[0].id.should.be.equal('1');
2526+
done();
2527+
});
2528+
});
2529+
});
2530+
});
2531+
});
2532+
});
2533+
24932534
it('should report error on duplicate keys', function(done) {
24942535
Post.create({title: 'd', content: 'DDD'}, function(err, post) {
24952536
Post.create({id: post.id, title: 'd', content: 'DDD'}, function(

0 commit comments

Comments
 (0)
Please sign in to comment.