@@ -10,6 +10,7 @@ import type {
1010 DeleteResult ,
1111 DistinctOptions ,
1212 Filter ,
13+ FindCursor ,
1314 FindOneAndDeleteOptions ,
1415 FindOneAndUpdateOptions ,
1516 FindOptions ,
@@ -86,6 +87,11 @@ export interface Model<TSchema extends BaseSchema, TOptions extends SchemaOption
8687 options ?: Omit < FindOptions < TSchema > , 'projection' > & { projection ?: TProjection }
8788 ) => Promise < ProjectionType < TSchema , TProjection > | null > ;
8889
90+ findCursor : < TProjection extends Projection < TSchema > | undefined > (
91+ filter : PaprFilter < TSchema > ,
92+ options ?: Omit < FindOptions < TSchema > , 'projection' > & { projection ?: TProjection }
93+ ) => Promise < FindCursor < ProjectionType < TSchema , TProjection > > > ;
94+
8995 findOne : < TProjection extends Projection < TSchema > | undefined > (
9096 filter : PaprFilter < TSchema > ,
9197 options ?: Omit < FindOptions < TSchema > , 'projection' > & { projection ?: TProjection }
@@ -158,6 +164,7 @@ export function abstract<TSchema extends BaseSchema, TOptions extends SchemaOpti
158164 deleteOne : abstractMethod ,
159165 find : abstractMethod ,
160166 findById : abstractMethod ,
167+ findCursor : abstractMethod ,
161168 findOne : abstractMethod ,
162169 findOneAndDelete : abstractMethod ,
163170 findOneAndUpdate : abstractMethod ,
@@ -668,6 +675,40 @@ export function build<TSchema extends BaseSchema, TOptions extends SchemaOptions
668675 }
669676 ) ;
670677
678+ /**
679+ * @description
680+ * Calls the MongoDB [`find()`](https://mongodb.github.io/node-mongodb-native/5.0/classes/Collection.html#find) method and returns the cursor.
681+ *
682+ * Useful when you want to process many records without loading them all into
683+ * memory at once.
684+ *
685+ * @param filter {PaprFilter<TSchema>}
686+ * @param [options] {FindOptions<TSchema>}
687+ *
688+ * @example
689+ * const cursor = await User.findCursor(
690+ * { active: true, email: { $exists: true } },
691+ * { projection: { email: 1 } }
692+ * )
693+ *
694+ * for await (const user of cursor) {
695+ * await notify(user.email);
696+ * }
697+ */
698+ model . findCursor = wrap ( model , async function findCursor <
699+ TProjection extends Projection < TSchema > | undefined
700+ > ( filter : PaprFilter < TSchema > , options ?: Omit < FindOptions < TSchema > , 'projection' > & { projection ?: TProjection } ) : Promise <
701+ FindCursor < ProjectionType < TSchema , TProjection > >
702+ > {
703+ return model . collection . find (
704+ filter as Filter < TSchema > ,
705+ {
706+ ...model . defaultOptions ,
707+ ...options ,
708+ } as FindOptions < TSchema >
709+ ) as FindCursor < ProjectionType < TSchema , TProjection > > ;
710+ } ) ;
711+
671712 /**
672713 * @description
673714 * Calls the MongoDB [`findOne()`](https://mongodb.github.io/node-mongodb-native/5.0/classes/Collection.html#findOne) method.
0 commit comments