diff --git a/index.ts b/index.ts index fb9e6fb..d4ccec3 100644 --- a/index.ts +++ b/index.ts @@ -2322,6 +2322,15 @@ const $filter = ( limit?: number, ) => new FilterOperator(inputExpr, asName, condExpr, limit); +type FindParams = [ + ArrayExpression, + string, + Expression, + number?, +]; + +const $find = (...args: FindParams) => $arrayElemAt($filter(...args), 0); + type FirstOperator = { $first: Expression, }; @@ -3789,6 +3798,8 @@ export = { facet: $facet, $filter, filter: $filter, + $find, + find: $find, $first, first: $first, $floor, diff --git a/pipeline.test.ts b/pipeline.test.ts index 2aff384..e775849 100644 --- a/pipeline.test.ts +++ b/pipeline.test.ts @@ -1062,6 +1062,24 @@ describe('aggregation', () => { }); }); }); + describe('$find', () => { + it('exports expected vars', () => { + expect($.find).toBeDefined(); + expect($.$find).toBeDefined(); + expect($.find).toStrictEqual($.$find); + }); + it('returns expected result', () => { + expect($.find('$input', 'foo', '$cond')).toEqual({ + $arrayElemAt: [{ + $filter: { + input: '$input', + as: 'foo', + cond: '$cond', + }, + }, 0], + }); + }); + }); describe('$first', () => { it('exports expected vars', () => { expect($.first).toBeDefined();