Skip to content

Commit

Permalink
feat: add collation support for MongoDB 3.4 and above (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsalvata authored and Garret Meier committed Dec 13, 2019
1 parent e09a447 commit caf9371
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,12 @@ See https://mongodb.github.io/node-mongodb-native/3.1/api/Cursor.html#addCursorF

See https://docs.mongodb.com/manual/reference/method/cursor.batchSize/

#### `cursor.collation(collationDocument)`

See https://docs.mongodb.com/manual/reference/method/cursor.collation/

Only supported with MongoDB 3.4 or higher.

#### `cursor.count()`

See https://docs.mongodb.com/manual/reference/method/cursor.count/
Expand Down
2 changes: 1 addition & 1 deletion lib/cursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Cursor extends Readable {

// Currently mongodb cursor options itcount, readPref, showDiskLoc are not implemented - and some other
// See https://docs.mongodb.com/manual/reference/method/js-cursor/
const operations = ['batchSize', 'hint', 'limit', 'maxTimeMS', 'max', 'min', 'skip', 'snapshot', 'sort']
const operations = ['batchSize', 'hint', 'limit', 'maxTimeMS', 'max', 'min', 'skip', 'snapshot', 'sort', 'collation'];

this._options = {};
this._flags = {};
Expand Down
34 changes: 26 additions & 8 deletions test/cursor.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { expect } = require('chai');
const { Writable } = require('stream');
const semver = require('semver');

const dropMongoDbCollections = require('drop-mongodb-collections');
const mongoist = require('../');
Expand All @@ -23,6 +24,8 @@ describe('cursor', function() {
name: 'Charmander', type: 'fire', level: 8,
}, {
name: 'Lapras', type: 'water', level: 12,
}, {
name: 'bulbasaur', type: 'amphybian', level: 7,
}]);
});

Expand Down Expand Up @@ -102,7 +105,20 @@ describe('cursor', function() {
.toArray();

const sortedNames = sortedDocs.map(doc => doc.name);
expect(sortedNames).to.deep.equal(['Charmander', 'Lapras', 'Squirtle', 'Starmie']);
expect(sortedNames).to.deep.equal(['Charmander', 'Lapras', 'Squirtle', 'Starmie', 'bulbasaur']);
});

it('should sort cursors case-insensitively in MongoDB v3.4 and later', async() => {
const buildInfo = await db.runCommand('buildInfo');
if (semver.gte(buildInfo.version, '3.4.0')) {
const sortedDocs = await db.a.findAsCursor()
.collation({locale: 'en'})
.sort({name: 1})
.toArray();

const sortedNames = sortedDocs.map(doc => doc.name);
expect(sortedNames).to.deep.equal(['bulbasaur', 'Charmander', 'Lapras', 'Squirtle', 'Starmie']);
}
});

it('should rewind a cursor', async() => {
Expand Down Expand Up @@ -130,26 +146,27 @@ describe('cursor', function() {
i++;
});

expect(i).to.equal(4);
expect(i).to.equal(5);
});

it('should map a cursor with map', async () => {
const names = await db.a.findAsCursor()
.sort({ name: 1})
.map((pkm) => pkm.name);

expect(names).to.deep.equal(['Charmander', 'Lapras', 'Squirtle', 'Starmie']);
expect(names).to.deep.equal(['Charmander', 'Lapras', 'Squirtle', 'Starmie', 'bulbasaur']);
});

it('should pass projections to findAsCursor', async () => {
const docs = await db.a.findAsCursor({}, { name: true, _id: false })
.toArray();

expect(docs).to.have.length(4);
expect(docs).to.have.length(5);
expect(docs).to.deep.include({ name: 'Charmander' });
expect(docs).to.deep.include({ name: 'Lapras' });
expect(docs).to.deep.include({ name: 'Squirtle' });
expect(docs).to.deep.include({ name: 'Starmie' });
expect(docs).to.deep.include({ name: 'bulbasaur' });
});

it('should return null for next if the cursor was closed', async() => {
Expand Down Expand Up @@ -197,7 +214,7 @@ describe('cursor', function() {

while ((doc = cursor.read()) !== null) {

expect(doc.name).to.be.oneOf(['Squirtle', 'Starmie', 'Charmander', 'Lapras']);
expect(doc.name).to.be.oneOf(['Squirtle', 'Starmie', 'Charmander', 'Lapras', 'bulbasaur']);
expect(doc).to.be.a('object');

runs++
Expand All @@ -210,7 +227,7 @@ describe('cursor', function() {

return new Promise((resolve) => {
cursor.on('end', function () {
expect(runs).to.equal(4);
expect(runs).to.equal(5);

resolve();
});
Expand All @@ -224,7 +241,7 @@ describe('cursor', function() {
db.a.findAsCursor()
.pipe(out)
.on('finish', () => {
expect(out._data).to.have.length(4);
expect(out._data).to.have.length(5);

resolve();
});
Expand Down Expand Up @@ -276,12 +293,13 @@ describe('cursor', function() {
for await (const doc of cursor) {
docs.push(doc);
}
expect(docs).to.have.length(4);
expect(docs).to.have.length(5);
expect(docs.map((doc) => doc.name)).to.have.members([
'Squirtle',
'Starmie',
'Charmander',
'Lapras',
'bulbasaur',
]);
});
});
Expand Down

0 comments on commit caf9371

Please sign in to comment.