Skip to content

Commit b70fd28

Browse files
committedJul 31, 2018
Add settings disableDefaultSort for find method
1 parent b79e263 commit b70fd28

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed
 

‎lib/mongodb.js

+16-4
Original file line numberDiff line numberDiff line change
@@ -936,10 +936,22 @@ 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 = this.settings.disableDefaultSort;
946+
if (options && options.hasOwnProperty('disableDefaultSort')) {
947+
disableDefaultSort = options.disableDefaultSort === true;
948+
} else if (disableDefaultSort !== false && modelClass.settings.hasOwnProperty('disableDefaultSort')) {
949+
disableDefaultSort = modelClass.settings.disableDefaultSort === true;
950+
} else if (disableDefaultSort === true) {
951+
disableDefaultSort = true;
952+
}
953+
954+
if (!order && !disableDefaultSort) {
943955
var idNames = this.idNames(model);
944956
if (idNames && idNames.length) {
945957
order = idNames;
@@ -966,7 +978,7 @@ MongoDB.prototype.buildSort = function(model, order) {
966978
sort[key] = 1;
967979
}
968980
}
969-
} else {
981+
} else if (!disableDefaultSort) {
970982
// order by _id by default
971983
sort = {_id: 1};
972984
}
@@ -1224,7 +1236,7 @@ MongoDB.prototype.all = function all(model, filter, options, callback) {
12241236

12251237
// don't apply sorting if dealing with a geo query
12261238
if (!hasNearFilter(filter.where)) {
1227-
var order = self.buildSort(model, filter.order);
1239+
var order = self.buildSort(model, filter.order, options);
12281240
cursor.sort(order);
12291241
}
12301242

‎test/mongodb.test.js

+28
Original file line numberDiff line numberDiff line change
@@ -2490,6 +2490,34 @@ describe('mongodb connector', function() {
24902490
});
24912491
});
24922492

2493+
it('find should not order by id if the order is not set for the query filter and settings.disableDefaultSort is true',
2494+
function(done) {
2495+
PostWithStringId.settings.disableDefaultSort = true;
2496+
2497+
PostWithStringId.create({ id: '2', title: 'c', content: 'CCC' }, function(err, post) {
2498+
PostWithStringId.create({ id: '1', title: 'd', content: 'DDD' }, function(err, post) {
2499+
PostWithStringId.find({}, function(err, posts) {
2500+
should.not.exist(err);
2501+
posts.length.should.be.equal(2);
2502+
posts[0].id.should.be.equal('2');
2503+
2504+
PostWithStringId.find({ limit: 1, offset: 0 }, function(err, posts) {
2505+
should.not.exist(err);
2506+
posts.length.should.be.equal(1);
2507+
posts[0].id.should.be.equal('2');
2508+
2509+
PostWithStringId.find({ limit: 1, offset: 1 }, function(err, posts) {
2510+
should.not.exist(err);
2511+
posts.length.should.be.equal(1);
2512+
posts[0].id.should.be.equal('1');
2513+
done();
2514+
});
2515+
});
2516+
});
2517+
});
2518+
});
2519+
});
2520+
24932521
it('should report error on duplicate keys', function(done) {
24942522
Post.create({title: 'd', content: 'DDD'}, function(err, post) {
24952523
Post.create({id: post.id, title: 'd', content: 'DDD'}, function(

0 commit comments

Comments
 (0)
Please sign in to comment.