Skip to content

Commit 2f053f0

Browse files
author
Srinand Balaji
authored
Merge pull request #307 from mixmaxhq/srinand/aggregate-hint
feat: add support for aggregation `hint`s
2 parents 1e89f97 + b90acd4 commit 2f053f0

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

src/aggregate.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const config = require('./config');
3333
* -previous {String} The value to start querying previous page.
3434
* -after {String} The _id to start querying the page.
3535
* -before {String} The _id to start querying previous page.
36+
* -options {Object} Aggregation options
3637
*/
3738
module.exports = async function aggregate(collection, params) {
3839
params = _.defaults(await sanitizeParams(collection, params), { aggregation: [] });
@@ -57,6 +58,10 @@ module.exports = async function aggregate(collection, params) {
5758
params.aggregation.splice(index + 1, 0, { $sort });
5859
params.aggregation.splice(index + 2, 0, { $limit: params.limit + 1 });
5960

61+
// Aggregation options:
62+
// https://mongodb.github.io/node-mongodb-native/3.6/api/Collection.html#aggregate
63+
// https://mongodb.github.io/node-mongodb-native/4.0/interfaces/aggregateoptions.html
64+
const options = params.options || {};
6065
/**
6166
* IMPORTANT
6267
*
@@ -66,7 +71,7 @@ module.exports = async function aggregate(collection, params) {
6671
*
6772
* See mongo documentation: https://docs.mongodb.com/manual/reference/collation/#collation-and-index-use
6873
*/
69-
const options = config.COLLATION ? { collation: config.COLLATION } : undefined;
74+
if (config.COLLATION) options.collation = config.COLLATION;
7075

7176
// Support both the native 'mongodb' driver and 'mongoist'. See:
7277
// https://www.npmjs.com/package/mongoist#cursor-operations

test/aggregate.test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,14 @@ describe('aggregate', () => {
8585
name: 'saturn',
8686
},
8787
]),
88+
t.db.collection('test_aggregation_lookup').ensureIndex(
89+
{
90+
name: 'text',
91+
},
92+
{
93+
name: 'test_index',
94+
}
95+
),
8896
t.db.collection('test_aggregation_sort').insert([
8997
{
9098
name: 'Alpha',
@@ -668,4 +676,33 @@ describe('aggregate', () => {
668676
]);
669677
});
670678
});
679+
680+
describe('aggregation options', () => {
681+
let spy;
682+
beforeEach(() => {
683+
spy = jest.spyOn(paging, 'aggregate');
684+
});
685+
686+
afterEach(() => {
687+
spy.mockRestore();
688+
});
689+
690+
it('invokes aggregate with a `hint` if one is passed in via params object', async () => {
691+
const collection = t.db.collection('test_aggregation_lookup');
692+
693+
await paging.aggregate(collection, {
694+
aggregation: [
695+
{
696+
$sort: { name: 1 },
697+
},
698+
],
699+
hint: 'test_index',
700+
});
701+
702+
expect(spy).toHaveBeenCalledWith(
703+
expect.any(Object),
704+
expect.objectContaining({ hint: 'test_index' })
705+
);
706+
});
707+
});
671708
});

0 commit comments

Comments
 (0)