Skip to content
This repository was archived by the owner on May 17, 2021. It is now read-only.

Commit 05516b4

Browse files
authored
fix(native-client): Set sort by using Cursor.sort method instead of passing it as a find option (#318)
1 parent ca81ec5 commit 05516b4

File tree

3 files changed

+48
-14
lines changed

3 files changed

+48
-14
lines changed

lib/native-client.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -659,15 +659,20 @@ class NativeClient extends EventEmitter {
659659
* @param {Object} options - The query options.
660660
* @param {Function} callback - The callback.
661661
*/
662-
find(ns, filter, options, callback) {
663-
this._collection(ns)
664-
.find(filter, options)
665-
.toArray((error, documents) => {
666-
if (error) {
667-
return callback(this._translateMessage(error));
668-
}
669-
callback(null, documents);
670-
});
662+
find(ns, filter = {}, options = {}, callback = () => {}) {
663+
// Workaround for https://jira.mongodb.org/browse/NODE-3173
664+
const sort = options.sort;
665+
delete options.sort;
666+
let cursor = this._collection(ns).find(filter, options);
667+
if (sort) {
668+
cursor = cursor.sort(sort);
669+
}
670+
cursor.toArray((error, documents) => {
671+
if (error) {
672+
return callback(this._translateMessage(error));
673+
}
674+
callback(null, documents);
675+
});
671676
}
672677

673678
/**

test/helper.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,19 @@ module.exports.connection = new Connection({
1818

1919
module.exports.insertTestDocuments = function(client, callback) {
2020
var collection = client.database.collection('test');
21-
collection.insertMany([{
22-
a: 1
23-
}, {
24-
a: 2
25-
}], callback);
21+
collection.insertMany(
22+
[
23+
{
24+
1: 'a',
25+
a: 1
26+
},
27+
{
28+
2: 'a',
29+
a: 2
30+
}
31+
],
32+
callback
33+
);
2634
};
2735

2836
module.exports.deleteTestDocuments = function(client, callback) {

test/native-client.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,27 @@ describe('NativeClient', function() {
409409
});
410410
});
411411
});
412+
413+
context('when array sort is provided', function() {
414+
it('returns documents with correct sort order', function(done) {
415+
const sort = [
416+
['2', -1],
417+
['1', -1]
418+
];
419+
420+
client.find(
421+
'data-service.test',
422+
{},
423+
{ sort },
424+
function(error, docs) {
425+
assert.strictEqual(null, error);
426+
expect(docs[0]).to.have.nested.property('2', 'a');
427+
expect(docs[1]).to.have.nested.property('1', 'a');
428+
done();
429+
}
430+
);
431+
});
432+
});
412433
});
413434

414435
describe('#fetch', function() {

0 commit comments

Comments
 (0)