@@ -79,7 +79,7 @@ import {
79
79
} from './operations/map_reduce' ;
80
80
import { OptionsOperation } from './operations/options_operation' ;
81
81
import { RenameOperation , RenameOptions } from './operations/rename' ;
82
- import { CollStatsOperation , CollStatsOptions } from './operations/stats' ;
82
+ import { CollStats , CollStatsOperation , CollStatsOptions } from './operations/stats' ;
83
83
import { executeOperation } from './operations/execute_operation' ;
84
84
import type { Db } from './db' ;
85
85
import type { OperationOptions , Hint } from './operations/operation' ;
@@ -89,7 +89,14 @@ import type { PkFactory } from './mongo_client';
89
89
import type { Logger , LoggerOptions } from './logger' ;
90
90
import { FindCursor } from './cursor/find_cursor' ;
91
91
import type { CountOptions } from './operations/count' ;
92
- import type { Filter , TODO_NODE_3286 , UpdateQuery , WithId , OptionalId } from './mongo_types' ;
92
+ import type {
93
+ Filter ,
94
+ TODO_NODE_3286 ,
95
+ UpdateQuery ,
96
+ WithId ,
97
+ OptionalId ,
98
+ FlattenIfArray
99
+ } from './mongo_types' ;
93
100
94
101
/** @public */
95
102
export interface ModifyResult < TSchema = Document > {
@@ -665,25 +672,42 @@ export class Collection<TSchema extends Document = Document> {
665
672
* @param options - Optional settings for the command
666
673
* @param callback - An optional callback, a Promise will be returned if none is provided
667
674
*/
668
- findOne ( ) : Promise < TSchema > ;
669
- findOne ( callback : Callback < TSchema > ) : void ;
670
- findOne ( filter : Filter < TSchema > ) : Promise < TSchema > ;
671
- findOne ( filter : Filter < TSchema > , callback ?: Callback < TSchema > ) : void ;
672
- findOne ( filter : Filter < TSchema > , options : FindOptions ) : Promise < TSchema > ;
673
- findOne ( filter : Filter < TSchema > , options : FindOptions , callback : Callback < TSchema > ) : void ;
675
+ findOne ( ) : Promise < TSchema | undefined > ;
676
+ findOne ( callback : Callback < TSchema | undefined > ) : void ;
677
+ findOne ( filter : Filter < TSchema > ) : Promise < TSchema | undefined > ;
678
+ findOne ( filter : Filter < TSchema > , callback : Callback < TSchema | undefined > ) : void ;
679
+ findOne ( filter : Filter < TSchema > , options : FindOptions < TSchema > ) : Promise < TSchema | undefined > ;
674
680
findOne (
675
- filter ?: Filter < TSchema > | Callback < TSchema > ,
676
- options ?: FindOptions | Callback < TSchema > ,
681
+ filter : Filter < TSchema > ,
682
+ options : FindOptions < TSchema > ,
683
+ callback : Callback < TSchema | undefined >
684
+ ) : void ;
685
+
686
+ // allow an override of the schema.
687
+ findOne < T = TSchema > ( ) : Promise < T | undefined > ;
688
+ findOne < T = TSchema > ( callback : Callback < T | undefined > ) : void ;
689
+ findOne < T = TSchema > ( filter : Filter < T > ) : Promise < T | undefined > ;
690
+ findOne < T = TSchema > ( filter : Filter < T > , options ?: FindOptions < T > ) : Promise < T | undefined > ;
691
+ findOne < T = TSchema > (
692
+ filter : Filter < T > ,
693
+ options ?: FindOptions < T > ,
694
+ callback ?: Callback < T | undefined >
695
+ ) : void ;
696
+
697
+ findOne (
698
+ filter ?: Filter < TSchema > | Callback < TSchema | undefined > ,
699
+ options ?: FindOptions < TSchema > | Callback < TSchema | undefined > ,
677
700
callback ?: Callback < TSchema >
678
- ) : Promise < TSchema > | void {
701
+ ) : Promise < TSchema | undefined > | void {
679
702
if ( callback !== undefined && typeof callback !== 'function' ) {
680
703
throw new MongoDriverError ( 'Third parameter to `findOne()` must be a callback or undefined' ) ;
681
704
}
682
705
683
706
if ( typeof filter === 'function' )
684
- ( callback = filter as Callback < Document > ) , ( filter = { } ) , ( options = { } ) ;
707
+ ( callback = filter as Callback < Document | undefined > ) , ( filter = { } ) , ( options = { } ) ;
685
708
if ( typeof options === 'function' ) ( callback = options ) , ( options = { } ) ;
686
- filter = filter || { } ;
709
+
710
+ filter ??= { } ;
687
711
688
712
return executeOperation (
689
713
getTopology ( this ) ,
@@ -692,7 +716,7 @@ export class Collection<TSchema extends Document = Document> {
692
716
filter ,
693
717
resolveOptions ( this , options )
694
718
) as TODO_NODE_3286 ,
695
- callback
719
+ callback as TODO_NODE_3286
696
720
) ;
697
721
}
698
722
@@ -702,8 +726,8 @@ export class Collection<TSchema extends Document = Document> {
702
726
* @param filter - The filter predicate. If unspecified, then all documents in the collection will match the predicate
703
727
*/
704
728
find ( ) : FindCursor < TSchema > ;
705
- find ( filter : Filter < TSchema > ) : FindCursor < TSchema > ;
706
- find ( filter : Filter < TSchema > , options : FindOptions < TSchema > ) : FindCursor < TSchema > ;
729
+ find ( filter : Filter < TSchema > , options ?: FindOptions < TSchema > ) : FindCursor < TSchema > ;
730
+ find < T = TSchema > ( filter : Filter < T > , options ? : FindOptions < T > ) : FindCursor < T > ;
707
731
find ( filter ?: Filter < TSchema > , options ?: FindOptions < TSchema > ) : FindCursor < TSchema > {
708
732
if ( arguments . length > 2 ) {
709
733
throw new MongoDriverError ( 'Third parameter to `collection.find()` must be undefined' ) ;
@@ -1054,6 +1078,7 @@ export class Collection<TSchema extends Document = Document> {
1054
1078
options : CountDocumentsOptions ,
1055
1079
callback : Callback < number >
1056
1080
) : void ;
1081
+ countDocuments ( filter : Filter < TSchema > , callback : Callback < number > ) : void ;
1057
1082
countDocuments (
1058
1083
filter ?: Document | CountDocumentsOptions | Callback < number > ,
1059
1084
options ?: CountDocumentsOptions | Callback < number > ,
@@ -1089,25 +1114,47 @@ export class Collection<TSchema extends Document = Document> {
1089
1114
* @param options - Optional settings for the command
1090
1115
* @param callback - An optional callback, a Promise will be returned if none is provided
1091
1116
*/
1092
- distinct < Key extends keyof WithId < TSchema > > ( key : Key ) : Promise < any [ ] > ;
1093
- distinct < Key extends keyof WithId < TSchema > > ( key : Key , callback : Callback < any [ ] > ) : void ;
1094
- distinct < Key extends keyof WithId < TSchema > > ( key : Key , filter : Filter < TSchema > ) : Promise < any [ ] > ;
1117
+ distinct < Key extends keyof WithId < TSchema > > (
1118
+ key : Key
1119
+ ) : Promise < Array < FlattenIfArray < WithId < TSchema > [ Key ] > > > ;
1120
+ distinct < Key extends keyof WithId < TSchema > > (
1121
+ key : Key ,
1122
+ callback : Callback < Array < FlattenIfArray < WithId < TSchema > [ Key ] > > >
1123
+ ) : void ;
1124
+ distinct < Key extends keyof WithId < TSchema > > (
1125
+ key : Key ,
1126
+ filter : Filter < TSchema >
1127
+ ) : Promise < Array < FlattenIfArray < WithId < TSchema > [ Key ] > > > ;
1095
1128
distinct < Key extends keyof WithId < TSchema > > (
1096
1129
key : Key ,
1097
1130
filter : Filter < TSchema > ,
1098
- callback : Callback < any [ ] >
1131
+ callback : Callback < Array < FlattenIfArray < WithId < TSchema > [ Key ] > > >
1099
1132
) : void ;
1100
1133
distinct < Key extends keyof WithId < TSchema > > (
1101
1134
key : Key ,
1102
1135
filter : Filter < TSchema > ,
1103
1136
options : DistinctOptions
1104
- ) : Promise < any [ ] > ;
1137
+ ) : Promise < Array < FlattenIfArray < WithId < TSchema > [ Key ] > > > ;
1105
1138
distinct < Key extends keyof WithId < TSchema > > (
1106
1139
key : Key ,
1107
1140
filter : Filter < TSchema > ,
1108
1141
options : DistinctOptions ,
1142
+ callback : Callback < Array < FlattenIfArray < WithId < TSchema > [ Key ] > > >
1143
+ ) : void ;
1144
+
1145
+ // Embedded documents overload
1146
+ distinct ( key : string ) : Promise < any [ ] > ;
1147
+ distinct ( key : string , callback : Callback < any [ ] > ) : void ;
1148
+ distinct ( key : string , filter : Filter < TSchema > ) : Promise < any [ ] > ;
1149
+ distinct ( key : string , filter : Filter < TSchema > , callback : Callback < any [ ] > ) : void ;
1150
+ distinct ( key : string , filter : Filter < TSchema > , options : DistinctOptions ) : Promise < any [ ] > ;
1151
+ distinct (
1152
+ key : string ,
1153
+ filter : Filter < TSchema > ,
1154
+ options : DistinctOptions ,
1109
1155
callback : Callback < any [ ] >
1110
1156
) : void ;
1157
+ // Implementation
1111
1158
distinct < Key extends keyof WithId < TSchema > > (
1112
1159
key : Key ,
1113
1160
filter ?: Filter < TSchema > | DistinctOptions | Callback < any [ ] > ,
@@ -1164,14 +1211,14 @@ export class Collection<TSchema extends Document = Document> {
1164
1211
* @param options - Optional settings for the command
1165
1212
* @param callback - An optional callback, a Promise will be returned if none is provided
1166
1213
*/
1167
- stats ( ) : Promise < Document > ;
1168
- stats ( callback : Callback < Document > ) : void ;
1169
- stats ( options : CollStatsOptions ) : Promise < Document > ;
1170
- stats ( options : CollStatsOptions , callback : Callback < Document > ) : void ;
1214
+ stats ( ) : Promise < CollStats > ;
1215
+ stats ( callback : Callback < CollStats > ) : void ;
1216
+ stats ( options : CollStatsOptions ) : Promise < CollStats > ;
1217
+ stats ( options : CollStatsOptions , callback : Callback < CollStats > ) : void ;
1171
1218
stats (
1172
- options ?: CollStatsOptions | Callback < Document > ,
1173
- callback ?: Callback < Document >
1174
- ) : Promise < Document > | void {
1219
+ options ?: CollStatsOptions | Callback < CollStats > ,
1220
+ callback ?: Callback < CollStats >
1221
+ ) : Promise < CollStats > | void {
1175
1222
if ( typeof options === 'function' ) ( callback = options ) , ( options = { } ) ;
1176
1223
options = options ?? { } ;
1177
1224
@@ -1372,27 +1419,27 @@ export class Collection<TSchema extends Document = Document> {
1372
1419
* @param callback - An optional callback, a Promise will be returned if none is provided
1373
1420
*/
1374
1421
mapReduce < TKey = any , TValue = any > (
1375
- map : string | MapFunction < TValue > ,
1422
+ map : string | MapFunction < TSchema > ,
1376
1423
reduce : string | ReduceFunction < TKey , TValue >
1377
1424
) : Promise < Document | Document [ ] > ;
1378
1425
mapReduce < TKey = any , TValue = any > (
1379
- map : string | MapFunction < TValue > ,
1426
+ map : string | MapFunction < TSchema > ,
1380
1427
reduce : string | ReduceFunction < TKey , TValue > ,
1381
1428
callback : Callback < Document | Document [ ] >
1382
1429
) : void ;
1383
1430
mapReduce < TKey = any , TValue = any > (
1384
- map : string | MapFunction < TValue > ,
1431
+ map : string | MapFunction < TSchema > ,
1385
1432
reduce : string | ReduceFunction < TKey , TValue > ,
1386
1433
options : MapReduceOptions < TKey , TValue >
1387
1434
) : Promise < Document | Document [ ] > ;
1388
1435
mapReduce < TKey = any , TValue = any > (
1389
- map : string | MapFunction < TValue > ,
1436
+ map : string | MapFunction < TSchema > ,
1390
1437
reduce : string | ReduceFunction < TKey , TValue > ,
1391
1438
options : MapReduceOptions < TKey , TValue > ,
1392
1439
callback : Callback < Document | Document [ ] >
1393
1440
) : void ;
1394
1441
mapReduce < TKey = any , TValue = any > (
1395
- map : string | MapFunction < TValue > ,
1442
+ map : string | MapFunction < TSchema > ,
1396
1443
reduce : string | ReduceFunction < TKey , TValue > ,
1397
1444
options ?: MapReduceOptions < TKey , TValue > | Callback < Document | Document [ ] > ,
1398
1445
callback ?: Callback < Document | Document [ ] >
0 commit comments