@@ -43,7 +43,7 @@ import {
4343 type EstimatedDocumentCountOptions
4444} from './operations/estimated_document_count' ;
4545import { autoConnect , executeOperation } from './operations/execute_operation' ;
46- import type { FindOptions } from './operations/find' ;
46+ import { type FindOneOptions , type FindOptions } from './operations/find' ;
4747import {
4848 FindOneAndDeleteOperation ,
4949 type FindOneAndDeleteOptions ,
@@ -536,25 +536,30 @@ export class Collection<TSchema extends Document = Document> {
536536 async findOne ( filter : Filter < TSchema > ) : Promise < WithId < TSchema > | null > ;
537537 async findOne (
538538 filter : Filter < TSchema > ,
539- options : Omit < FindOptions , 'timeoutMode' > & Abortable
539+ options : Omit < FindOneOptions , 'timeoutMode' > & Abortable
540540 ) : Promise < WithId < TSchema > | null > ;
541541
542542 // allow an override of the schema.
543543 async findOne < T = TSchema > ( ) : Promise < T | null > ;
544544 async findOne < T = TSchema > ( filter : Filter < TSchema > ) : Promise < T | null > ;
545545 async findOne < T = TSchema > (
546546 filter : Filter < TSchema > ,
547- options ?: Omit < FindOptions , 'timeoutMode' > & Abortable
547+ options ?: Omit < FindOneOptions , 'timeoutMode' > & Abortable
548548 ) : Promise < T | null > ;
549549
550550 async findOne (
551551 filter : Filter < TSchema > = { } ,
552- options : FindOptions & Abortable = { }
552+ options : Omit < FindOneOptions , 'timeoutMode' > & Abortable = { }
553553 ) : Promise < WithId < TSchema > | null > {
554- const cursor = this . find ( filter , options ) . limit ( - 1 ) . batchSize ( 1 ) ;
555- const res = await cursor . next ( ) ;
554+ // Explicitly set the limit to 1 and singleBatch to true for all commands, per the spec.
555+ // noCursorTimeout must be unset as well as batchSize.
556+ // See: https://github.com/mongodb/specifications/blob/master/source/crud/crud.md#findone-api-details
557+ const { batchSize : _batchSize , noCursorTimeout : _noCursorTimeout , ...opts } = options ;
558+ opts . singleBatch = true ;
559+ const cursor = this . find ( filter , opts ) . limit ( 1 ) ;
560+ const result = await cursor . next ( ) ;
556561 await cursor . close ( ) ;
557- return res ;
562+ return result ;
558563 }
559564
560565 /**
0 commit comments