@@ -43,7 +43,7 @@ import {
43
43
type EstimatedDocumentCountOptions
44
44
} from './operations/estimated_document_count' ;
45
45
import { autoConnect , executeOperation } from './operations/execute_operation' ;
46
- import type { FindOptions } from './operations/find' ;
46
+ import { type FindOneOptions , type FindOptions } from './operations/find' ;
47
47
import {
48
48
FindOneAndDeleteOperation ,
49
49
type FindOneAndDeleteOptions ,
@@ -536,25 +536,30 @@ export class Collection<TSchema extends Document = Document> {
536
536
async findOne ( filter : Filter < TSchema > ) : Promise < WithId < TSchema > | null > ;
537
537
async findOne (
538
538
filter : Filter < TSchema > ,
539
- options : Omit < FindOptions , 'timeoutMode' > & Abortable
539
+ options : Omit < FindOneOptions , 'timeoutMode' > & Abortable
540
540
) : Promise < WithId < TSchema > | null > ;
541
541
542
542
// allow an override of the schema.
543
543
async findOne < T = TSchema > ( ) : Promise < T | null > ;
544
544
async findOne < T = TSchema > ( filter : Filter < TSchema > ) : Promise < T | null > ;
545
545
async findOne < T = TSchema > (
546
546
filter : Filter < TSchema > ,
547
- options ?: Omit < FindOptions , 'timeoutMode' > & Abortable
547
+ options ?: Omit < FindOneOptions , 'timeoutMode' > & Abortable
548
548
) : Promise < T | null > ;
549
549
550
550
async findOne (
551
551
filter : Filter < TSchema > = { } ,
552
- options : FindOptions & Abortable = { }
552
+ options : Omit < FindOneOptions , 'timeoutMode' > & Abortable = { }
553
553
) : 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 ( ) ;
556
561
await cursor . close ( ) ;
557
- return res ;
562
+ return result ;
558
563
}
559
564
560
565
/**
0 commit comments