mongoose 6.1.8 → 6.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/.eslintrc.json +150 -0
  2. package/CHANGELOG.md +50 -0
  3. package/dist/browser.umd.js +191 -187
  4. package/lib/aggregate.js +1 -1
  5. package/lib/cast/objectid.js +1 -2
  6. package/lib/cast.js +21 -8
  7. package/lib/connection.js +33 -3
  8. package/lib/document.js +84 -65
  9. package/lib/error/index.js +11 -0
  10. package/lib/error/syncIndexes.js +30 -0
  11. package/lib/helpers/clone.js +40 -27
  12. package/lib/helpers/common.js +2 -2
  13. package/lib/helpers/getFunctionName.js +6 -4
  14. package/lib/helpers/isMongooseObject.js +9 -8
  15. package/lib/helpers/isObject.js +4 -4
  16. package/lib/helpers/model/discriminator.js +2 -1
  17. package/lib/helpers/path/parentPaths.js +10 -5
  18. package/lib/helpers/populate/assignRawDocsToIdStructure.js +4 -2
  19. package/lib/helpers/populate/assignVals.js +8 -4
  20. package/lib/helpers/populate/getModelsMapForPopulate.js +6 -6
  21. package/lib/helpers/populate/markArraySubdocsPopulated.js +3 -1
  22. package/lib/helpers/populate/modelNamesFromRefPath.js +6 -5
  23. package/lib/helpers/query/cast$expr.js +284 -0
  24. package/lib/helpers/query/castUpdate.js +6 -2
  25. package/lib/helpers/schema/applyPlugins.js +11 -0
  26. package/lib/helpers/schema/getPath.js +4 -2
  27. package/lib/helpers/setDefaultsOnInsert.js +16 -7
  28. package/lib/helpers/timestamps/setupTimestamps.js +3 -8
  29. package/lib/helpers/update/applyTimestampsToChildren.js +2 -2
  30. package/lib/helpers/update/castArrayFilters.js +1 -1
  31. package/lib/helpers/update/updatedPathsByArrayFilter.js +1 -1
  32. package/lib/index.js +16 -40
  33. package/lib/internal.js +1 -1
  34. package/lib/model.js +37 -18
  35. package/lib/plugins/trackTransaction.js +4 -3
  36. package/lib/query.js +15 -13
  37. package/lib/queryhelpers.js +1 -1
  38. package/lib/schema/SubdocumentPath.js +1 -1
  39. package/lib/schema/array.js +18 -16
  40. package/lib/schema/documentarray.js +6 -9
  41. package/lib/schema/objectid.js +1 -1
  42. package/lib/schema.js +5 -4
  43. package/lib/schematype.js +74 -26
  44. package/lib/types/ArraySubdocument.js +2 -1
  45. package/lib/types/DocumentArray/index.js +9 -26
  46. package/lib/types/DocumentArray/isMongooseDocumentArray.js +5 -0
  47. package/lib/types/DocumentArray/methods/index.js +15 -3
  48. package/lib/types/array/index.js +21 -20
  49. package/lib/types/array/isMongooseArray.js +5 -0
  50. package/lib/types/array/methods/index.js +12 -12
  51. package/lib/utils.js +15 -8
  52. package/package.json +24 -156
  53. package/tools/repl.js +1 -1
  54. package/tsconfig.json +10 -0
  55. package/{index.d.ts → types/index.d.ts} +146 -86
@@ -74,8 +74,8 @@ declare module 'mongoose' {
74
74
  * the model's schema except the `_id` index, and build any indexes that
75
75
  * are in your schema but not in MongoDB.
76
76
  */
77
- export function syncIndexes(options?: Record<string, unknown>): Promise<Array<string>>;
78
- export function syncIndexes(options: Record<string, unknown> | null, callback: Callback<Array<string>>): void;
77
+ export function syncIndexes(options?: SyncIndexesOptions): Promise<ConnectionSyncIndexesResult>;
78
+ export function syncIndexes(options: SyncIndexesOptions | null, callback: Callback<ConnectionSyncIndexesResult>): void;
79
79
 
80
80
  /* Tells `sanitizeFilter()` to skip the given object when filtering out potential query selector injection attacks.
81
81
  * Use this method when you have a known query selector that you want to use. */
@@ -121,6 +121,7 @@ declare module 'mongoose' {
121
121
  * Returns true if Mongoose can cast the given value to an ObjectId, or
122
122
  * false otherwise.
123
123
  */
124
+ export function isValidObjectId(v: Types.ObjectId): true;
124
125
  export function isValidObjectId(v: any): boolean;
125
126
 
126
127
  export function model<T>(name: string, schema?: Schema<T, any, any> | Schema<T & Document, any, any>, collection?: string, options?: CompileModelOptions): Model<T>;
@@ -165,6 +166,7 @@ declare module 'mongoose' {
165
166
  export const version: string;
166
167
 
167
168
  export type CastError = Error.CastError;
169
+ export type SyncIndexesError = Error.SyncIndexesError;
168
170
 
169
171
  type Mongoose = typeof mongoose;
170
172
 
@@ -260,6 +262,9 @@ declare module 'mongoose' {
260
262
  /** true by default, may be `false`, `true`, or `'throw'`. Sets the default strict mode for schemas. */
261
263
  strict?: boolean | 'throw';
262
264
 
265
+ /** true by default. set to `false` to allow populating paths that aren't in the schema */
266
+ strictPopulate?: boolean;
267
+
263
268
  /**
264
269
  * false by default, may be `false`, `true`, or `'throw'`. Sets the default
265
270
  * [strictQuery](https://mongoosejs.com/docs/guide.html#strictQuery) mode for schemas.
@@ -304,7 +309,7 @@ declare module 'mongoose' {
304
309
  close(force?: boolean): Promise<void>;
305
310
 
306
311
  /** Retrieves a collection, creating it if not cached. */
307
- collection(name: string, options?: mongodb.CreateCollectionOptions): Collection;
312
+ collection<T = AnyObject>(name: string, options?: mongodb.CreateCollectionOptions): Collection<T>;
308
313
 
309
314
  /** A hash of the collections associated with this connection */
310
315
  collections: { [index: string]: Collection };
@@ -320,9 +325,9 @@ declare module 'mongoose' {
320
325
  * with specified options. Used to create [capped collections](https://docs.mongodb.com/manual/core/capped-collections/)
321
326
  * and [views](https://docs.mongodb.com/manual/core/views/) from mongoose.
322
327
  */
323
- createCollection(name: string, options?: mongodb.CreateCollectionOptions): Promise<mongodb.Collection>;
324
- createCollection(name: string, cb: Callback<mongodb.Collection>): void;
325
- createCollection(name: string, options: mongodb.CreateCollectionOptions, cb?: Callback<mongodb.Collection>): Promise<mongodb.Collection>;
328
+ createCollection<T = AnyObject>(name: string, options?: mongodb.CreateCollectionOptions): Promise<mongodb.Collection<T>>;
329
+ createCollection<T = AnyObject>(name: string, cb: Callback<mongodb.Collection<T>>): void;
330
+ createCollection<T = AnyObject>(name: string, options: mongodb.CreateCollectionOptions, cb?: Callback<mongodb.Collection<T>>): Promise<mongodb.Collection<T>>;
326
331
 
327
332
  /**
328
333
  * Removes the model named `name` from this connection, if it exists. You can
@@ -442,8 +447,8 @@ declare module 'mongoose' {
442
447
  * the model's schema except the `_id` index, and build any indexes that
443
448
  * are in your schema but not in MongoDB.
444
449
  */
445
- syncIndexes(options?: Record<string, unknown>): Promise<Array<string>>;
446
- syncIndexes(options: Record<string, unknown> | null, callback: Callback<Array<string>>): void;
450
+ syncIndexes(options?: SyncIndexesOptions): Promise<ConnectionSyncIndexesResult>;
451
+ syncIndexes(options: SyncIndexesOptions | null, callback: Callback<ConnectionSyncIndexesResult>): void;
447
452
 
448
453
  /**
449
454
  * _Requires MongoDB >= 3.6.0._ Executes the wrapped async function
@@ -467,7 +472,7 @@ declare module 'mongoose' {
467
472
  * section collection.js
468
473
  * http://mongoosejs.com/docs/api.html#collection-js
469
474
  */
470
- interface CollectionBase extends mongodb.Collection {
475
+ interface CollectionBase<T> extends mongodb.Collection<T> {
471
476
  /*
472
477
  * Abstract methods. Some of these are already defined on the
473
478
  * mongodb.Collection interface so they've been commented out.
@@ -489,7 +494,7 @@ declare module 'mongoose' {
489
494
  * http://mongoosejs.com/docs/api.html#drivers-node-mongodb-native-collection-js
490
495
  */
491
496
  let Collection: Collection;
492
- interface Collection extends CollectionBase {
497
+ interface Collection<T = AnyObject> extends CollectionBase<T> {
493
498
  /**
494
499
  * Collection constructor
495
500
  * @param name name of the collection
@@ -497,7 +502,7 @@ declare module 'mongoose' {
497
502
  * @param opts optional collection options
498
503
  */
499
504
  // eslint-disable-next-line @typescript-eslint/no-misused-new
500
- new(name: string, conn: Connection, opts?: any): Collection;
505
+ new(name: string, conn: Connection, opts?: any): Collection<T>;
501
506
  /** Formatter for debug print args */
502
507
  $format(arg: any): string;
503
508
  /** Debug print helper */
@@ -759,7 +764,7 @@ declare module 'mongoose' {
759
764
 
760
765
  type Require_id<T> = T extends { _id?: any } ? (T & { _id: T['_id'] }) : (T & { _id: Types.ObjectId });
761
766
 
762
- export type HydratedDocument<DocType, TMethods = {}, TVirtuals = {}> = DocType extends Document ? Require_id<DocType> : (Document<unknown, any, DocType> & Require_id<DocType> & TVirtuals & TMethods);
767
+ export type HydratedDocument<DocType, TMethodsAndOverrides = {}, TVirtuals = {}> = DocType extends Document ? Require_id<DocType> : (Document<unknown, any, DocType> & Require_id<DocType> & TVirtuals & TMethodsAndOverrides);
763
768
 
764
769
  interface IndexesDiff {
765
770
  /** Indexes that would be created in mongodb. */
@@ -768,9 +773,15 @@ declare module 'mongoose' {
768
773
  toDrop: Array<any>
769
774
  }
770
775
 
776
+ interface ModifyResult<T> {
777
+ value: Require_id<T> | null;
778
+ lastErrorObject?: mongodb.Document;
779
+ ok: 0 | 1;
780
+ }
781
+
771
782
  export const Model: Model<any>;
772
- interface Model<T, TQueryHelpers = {}, TMethods = {}, TVirtuals = {}> extends NodeJS.EventEmitter, AcceptsDiscriminator {
773
- new<DocType = AnyKeys<T> & AnyObject>(doc?: DocType, fields?: any | null, options?: boolean | AnyObject): HydratedDocument<T, TMethods, TVirtuals>;
783
+ interface Model<T, TQueryHelpers = {}, TMethodsAndOverrides = {}, TVirtuals = {}> extends NodeJS.EventEmitter, AcceptsDiscriminator {
784
+ new<DocType = AnyKeys<T> & AnyObject>(doc?: DocType, fields?: any | null, options?: boolean | AnyObject): HydratedDocument<T, TMethodsAndOverrides, TVirtuals>;
774
785
 
775
786
  aggregate<R = any>(pipeline?: PipelineStage[], options?: mongodb.AggregateOptions, callback?: Callback<R[]>): Aggregate<Array<R>>;
776
787
  aggregate<R = any>(pipeline: PipelineStage[], cb: Function): Aggregate<Array<R>>;
@@ -799,37 +810,37 @@ declare module 'mongoose' {
799
810
  * sending multiple `save()` calls because with `bulkSave()` there is only one
800
811
  * network round trip to the MongoDB server.
801
812
  */
802
- bulkSave(documents: Array<Document>): Promise<mongodb.BulkWriteResult>;
813
+ bulkSave(documents: Array<Document>, options?: mongodb.BulkWriteOptions): Promise<mongodb.BulkWriteResult>;
803
814
 
804
815
  /** Collection the model uses. */
805
816
  collection: Collection;
806
817
 
807
818
  /** Creates a `count` query: counts the number of documents that match `filter`. */
808
- count(callback?: Callback<number>): QueryWithHelpers<number, HydratedDocument<T, TMethods, TVirtuals>, TQueryHelpers, T>;
809
- count(filter: FilterQuery<T>, callback?: Callback<number>): QueryWithHelpers<number, HydratedDocument<T, TMethods, TVirtuals>, TQueryHelpers, T>;
819
+ count(callback?: Callback<number>): QueryWithHelpers<number, HydratedDocument<T, TMethodsAndOverrides, TVirtuals>, TQueryHelpers, T>;
820
+ count(filter: FilterQuery<T>, callback?: Callback<number>): QueryWithHelpers<number, HydratedDocument<T, TMethodsAndOverrides, TVirtuals>, TQueryHelpers, T>;
810
821
 
811
822
  /** Creates a `countDocuments` query: counts the number of documents that match `filter`. */
812
- countDocuments(callback?: Callback<number>): QueryWithHelpers<number, HydratedDocument<T, TMethods, TVirtuals>, TQueryHelpers, T>;
813
- countDocuments(filter: FilterQuery<T>, options?: QueryOptions, callback?: Callback<number>): QueryWithHelpers<number, HydratedDocument<T, TMethods, TVirtuals>, TQueryHelpers, T>;
823
+ countDocuments(callback?: Callback<number>): QueryWithHelpers<number, HydratedDocument<T, TMethodsAndOverrides, TVirtuals>, TQueryHelpers, T>;
824
+ countDocuments(filter: FilterQuery<T>, options?: QueryOptions, callback?: Callback<number>): QueryWithHelpers<number, HydratedDocument<T, TMethodsAndOverrides, TVirtuals>, TQueryHelpers, T>;
814
825
 
815
826
  /** Creates a new document or documents */
816
- create(docs: (AnyKeys<T> | AnyObject)[], options?: SaveOptions): Promise<HydratedDocument<T, TMethods, TVirtuals>[]>;
817
- create(docs: (AnyKeys<T> | AnyObject)[], callback: Callback<HydratedDocument<T, TMethods, TVirtuals>[]>): void;
818
- create(doc: AnyKeys<T> | AnyObject): Promise<HydratedDocument<T, TMethods, TVirtuals>>;
819
- create(doc: AnyKeys<T> | AnyObject, callback: Callback<HydratedDocument<T, TMethods, TVirtuals>>): void;
820
- create<DocContents = AnyKeys<T>>(docs: DocContents[], options?: SaveOptions): Promise<HydratedDocument<T, TMethods, TVirtuals>[]>;
821
- create<DocContents = AnyKeys<T>>(docs: DocContents[], callback: Callback<HydratedDocument<T, TMethods, TVirtuals>[]>): void;
822
- create<DocContents = AnyKeys<T>>(doc: DocContents): Promise<HydratedDocument<T, TMethods, TVirtuals>>;
823
- create<DocContents = AnyKeys<T>>(...docs: DocContents[]): Promise<HydratedDocument<T, TMethods, TVirtuals>[]>;
824
- create<DocContents = AnyKeys<T>>(doc: DocContents, callback: Callback<HydratedDocument<T, TMethods, TVirtuals>>): void;
827
+ create(docs: (AnyKeys<T> | AnyObject)[], options?: SaveOptions): Promise<HydratedDocument<T, TMethodsAndOverrides, TVirtuals>[]>;
828
+ create(docs: (AnyKeys<T> | AnyObject)[], callback: Callback<HydratedDocument<T, TMethodsAndOverrides, TVirtuals>[]>): void;
829
+ create(doc: AnyKeys<T> | AnyObject): Promise<HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>;
830
+ create(doc: AnyKeys<T> | AnyObject, callback: Callback<HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>): void;
831
+ create<DocContents = AnyKeys<T>>(docs: DocContents[], options?: SaveOptions): Promise<HydratedDocument<T, TMethodsAndOverrides, TVirtuals>[]>;
832
+ create<DocContents = AnyKeys<T>>(docs: DocContents[], callback: Callback<HydratedDocument<T, TMethodsAndOverrides, TVirtuals>[]>): void;
833
+ create<DocContents = AnyKeys<T>>(doc: DocContents): Promise<HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>;
834
+ create<DocContents = AnyKeys<T>>(...docs: DocContents[]): Promise<HydratedDocument<T, TMethodsAndOverrides, TVirtuals>[]>;
835
+ create<DocContents = AnyKeys<T>>(doc: DocContents, callback: Callback<HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>): void;
825
836
 
826
837
  /**
827
838
  * Create the collection for this model. By default, if no indexes are specified,
828
839
  * mongoose will not create the collection for the model until any documents are
829
840
  * created. Use this method to create the collection explicitly.
830
841
  */
831
- createCollection(options?: mongodb.CreateCollectionOptions): Promise<mongodb.Collection>;
832
- createCollection(options: mongodb.CreateCollectionOptions | null, callback: Callback<mongodb.Collection>): void;
842
+ createCollection<T>(options?: mongodb.CreateCollectionOptions): Promise<mongodb.Collection<T>>;
843
+ createCollection<T>(options: mongodb.CreateCollectionOptions | null, callback: Callback<mongodb.Collection<T>>): void;
833
844
 
834
845
  /**
835
846
  * Similar to `ensureIndexes()`, except for it uses the [`createIndex`](http://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html#createIndex)
@@ -846,18 +857,18 @@ declare module 'mongoose' {
846
857
  * Behaves like `remove()`, but deletes all documents that match `conditions`
847
858
  * regardless of the `single` option.
848
859
  */
849
- deleteMany(filter?: FilterQuery<T>, options?: QueryOptions, callback?: CallbackWithoutResult): QueryWithHelpers<mongodb.DeleteResult, HydratedDocument<T, TMethods, TVirtuals>, TQueryHelpers, T>;
850
- deleteMany(filter: FilterQuery<T>, callback: CallbackWithoutResult): QueryWithHelpers<mongodb.DeleteResult, HydratedDocument<T, TMethods, TVirtuals>, TQueryHelpers, T>;
851
- deleteMany(callback: CallbackWithoutResult): QueryWithHelpers<mongodb.DeleteResult, HydratedDocument<T, TMethods, TVirtuals>, TQueryHelpers, T>;
860
+ deleteMany(filter?: FilterQuery<T>, options?: QueryOptions, callback?: CallbackWithoutResult): QueryWithHelpers<mongodb.DeleteResult, HydratedDocument<T, TMethodsAndOverrides, TVirtuals>, TQueryHelpers, T>;
861
+ deleteMany(filter: FilterQuery<T>, callback: CallbackWithoutResult): QueryWithHelpers<mongodb.DeleteResult, HydratedDocument<T, TMethodsAndOverrides, TVirtuals>, TQueryHelpers, T>;
862
+ deleteMany(callback: CallbackWithoutResult): QueryWithHelpers<mongodb.DeleteResult, HydratedDocument<T, TMethodsAndOverrides, TVirtuals>, TQueryHelpers, T>;
852
863
 
853
864
  /**
854
865
  * Deletes the first document that matches `conditions` from the collection.
855
866
  * Behaves like `remove()`, but deletes at most one document regardless of the
856
867
  * `single` option.
857
868
  */
858
- deleteOne(filter?: FilterQuery<T>, options?: QueryOptions, callback?: CallbackWithoutResult): QueryWithHelpers<mongodb.DeleteResult, HydratedDocument<T, TMethods, TVirtuals>, TQueryHelpers, T>;
859
- deleteOne(filter: FilterQuery<T>, callback: CallbackWithoutResult): QueryWithHelpers<mongodb.DeleteResult, HydratedDocument<T, TMethods, TVirtuals>, TQueryHelpers, T>;
860
- deleteOne(callback: CallbackWithoutResult): QueryWithHelpers<mongodb.DeleteResult, HydratedDocument<T, TMethods, TVirtuals>, TQueryHelpers, T>;
869
+ deleteOne(filter?: FilterQuery<T>, options?: QueryOptions, callback?: CallbackWithoutResult): QueryWithHelpers<mongodb.DeleteResult, HydratedDocument<T, TMethodsAndOverrides, TVirtuals>, TQueryHelpers, T>;
870
+ deleteOne(filter: FilterQuery<T>, callback: CallbackWithoutResult): QueryWithHelpers<mongodb.DeleteResult, HydratedDocument<T, TMethodsAndOverrides, TVirtuals>, TQueryHelpers, T>;
871
+ deleteOne(callback: CallbackWithoutResult): QueryWithHelpers<mongodb.DeleteResult, HydratedDocument<T, TMethodsAndOverrides, TVirtuals>, TQueryHelpers, T>;
861
872
 
862
873
  /**
863
874
  * Sends `createIndex` commands to mongo for each index declared in the schema.
@@ -877,16 +888,16 @@ declare module 'mongoose' {
877
888
  * equivalent to `findOne({ _id: id })`. If you want to query by a document's
878
889
  * `_id`, use `findById()` instead of `findOne()`.
879
890
  */
880
- findById(id: any, projection?: any | null, options?: QueryOptions | null, callback?: Callback<HydratedDocument<T, TMethods, TVirtuals> | null>): QueryWithHelpers<HydratedDocument<T, TMethods, TVirtuals> | null, HydratedDocument<T, TMethods, TVirtuals>, TQueryHelpers, T>;
891
+ findById<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(id: any, projection?: any | null, options?: QueryOptions | null, callback?: Callback<ResultDoc | null>): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, T>;
881
892
 
882
893
  /** Finds one document. */
883
- findOne(filter?: FilterQuery<T>, projection?: any | null, options?: QueryOptions | null, callback?: Callback<HydratedDocument<T, TMethods, TVirtuals> | null>): QueryWithHelpers<HydratedDocument<T, TMethods, TVirtuals> | null, HydratedDocument<T, TMethods, TVirtuals>, TQueryHelpers, T>;
894
+ findOne<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(filter?: FilterQuery<T>, projection?: any | null, options?: QueryOptions | null, callback?: Callback<ResultDoc | null>): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, T>;
884
895
 
885
896
  /**
886
897
  * Shortcut for creating a new Document from existing raw data, pre-saved in the DB.
887
898
  * The document returned has no paths marked as modified initially.
888
899
  */
889
- hydrate(obj: any): HydratedDocument<T, TMethods, TVirtuals>;
900
+ hydrate(obj: any): HydratedDocument<T, TMethodsAndOverrides, TVirtuals>;
890
901
 
891
902
  /**
892
903
  * This function is responsible for building [indexes](https://docs.mongodb.com/manual/indexes/),
@@ -896,15 +907,15 @@ declare module 'mongoose' {
896
907
  * [`connection.model()`](/docs/api.html#connection_Connection-model), so you
897
908
  * don't need to call it.
898
909
  */
899
- init(callback?: CallbackWithoutResult): Promise<HydratedDocument<T, TMethods, TVirtuals>>;
910
+ init(callback?: CallbackWithoutResult): Promise<HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>;
900
911
 
901
912
  /** Inserts one or more new documents as a single `insertMany` call to the MongoDB server. */
902
913
  insertMany(docs: Array<AnyKeys<T> | AnyObject>, options: InsertManyOptions & { rawResult: true }): Promise<InsertManyResult>;
903
- insertMany(docs: Array<AnyKeys<T> | AnyObject>, options?: InsertManyOptions): Promise<Array<HydratedDocument<T, TMethods, TVirtuals>>>;
914
+ insertMany(docs: Array<AnyKeys<T> | AnyObject>, options?: InsertManyOptions): Promise<Array<HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>>;
904
915
  insertMany(doc: AnyKeys<T> | AnyObject, options: InsertManyOptions & { rawResult: true }): Promise<InsertManyResult>;
905
- insertMany(doc: AnyKeys<T> | AnyObject, options?: InsertManyOptions): Promise<HydratedDocument<T, TMethods, TVirtuals>[]>;
906
- insertMany(doc: AnyKeys<T> | AnyObject, options?: InsertManyOptions, callback?: Callback<HydratedDocument<T, TMethods, TVirtuals>[] | InsertManyResult>): void;
907
- insertMany(docs: Array<AnyKeys<T> | AnyObject>, options?: InsertManyOptions, callback?: Callback<Array<HydratedDocument<T, TMethods, TVirtuals>> | InsertManyResult>): void;
916
+ insertMany(doc: AnyKeys<T> | AnyObject, options?: InsertManyOptions): Promise<HydratedDocument<T, TMethodsAndOverrides, TVirtuals>[]>;
917
+ insertMany(doc: AnyKeys<T> | AnyObject, options?: InsertManyOptions, callback?: Callback<HydratedDocument<T, TMethodsAndOverrides, TVirtuals>[] | InsertManyResult>): void;
918
+ insertMany(docs: Array<AnyKeys<T> | AnyObject>, options?: InsertManyOptions, callback?: Callback<Array<HydratedDocument<T, TMethodsAndOverrides, TVirtuals>> | InsertManyResult>): void;
908
919
 
909
920
  /**
910
921
  * Lists the indexes currently defined in MongoDB. This may or may not be
@@ -920,9 +931,9 @@ declare module 'mongoose' {
920
931
 
921
932
  /** Populates document references. */
922
933
  populate(docs: Array<any>, options: PopulateOptions | Array<PopulateOptions> | string,
923
- callback?: Callback<(HydratedDocument<T, TMethods, TVirtuals>)[]>): Promise<Array<HydratedDocument<T, TMethods, TVirtuals>>>;
934
+ callback?: Callback<(HydratedDocument<T, TMethodsAndOverrides, TVirtuals>)[]>): Promise<Array<HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>>;
924
935
  populate(doc: any, options: PopulateOptions | Array<PopulateOptions> | string,
925
- callback?: Callback<HydratedDocument<T, TMethods, TVirtuals>>): Promise<HydratedDocument<T, TMethods, TVirtuals>>;
936
+ callback?: Callback<HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>): Promise<HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>;
926
937
 
927
938
  /**
928
939
  * Makes the indexes in MongoDB match the indexes defined in this model's
@@ -957,7 +968,7 @@ declare module 'mongoose' {
957
968
  watch<ResultType = any>(pipeline?: Array<Record<string, unknown>>, options?: mongodb.ChangeStreamOptions): mongodb.ChangeStream<ResultType>;
958
969
 
959
970
  /** Adds a `$where` clause to this query */
960
- $where(argument: string | Function): QueryWithHelpers<Array<HydratedDocument<T, TMethods, TVirtuals>>, HydratedDocument<T, TMethods, TVirtuals>, TQueryHelpers, T>;
971
+ $where(argument: string | Function): QueryWithHelpers<Array<HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>, HydratedDocument<T, TMethodsAndOverrides, TVirtuals>, TQueryHelpers, T>;
961
972
 
962
973
  /** Registered discriminators for this model. */
963
974
  discriminators: { [name: string]: Model<any> } | undefined;
@@ -966,51 +977,51 @@ declare module 'mongoose' {
966
977
  translateAliases(raw: any): any;
967
978
 
968
979
  /** Creates a `distinct` query: returns the distinct values of the given `field` that match `filter`. */
969
- distinct(field: string, filter?: FilterQuery<T>, callback?: Callback<number>): QueryWithHelpers<Array<any>, HydratedDocument<T, TMethods, TVirtuals>, TQueryHelpers, T>;
980
+ distinct(field: string, filter?: FilterQuery<T>, callback?: Callback<number>): QueryWithHelpers<Array<any>, HydratedDocument<T, TMethodsAndOverrides, TVirtuals>, TQueryHelpers, T>;
970
981
 
971
982
  /** Creates a `estimatedDocumentCount` query: counts the number of documents in the collection. */
972
- estimatedDocumentCount(options?: QueryOptions, callback?: Callback<number>): QueryWithHelpers<number, HydratedDocument<T, TMethods, TVirtuals>, TQueryHelpers, T>;
983
+ estimatedDocumentCount(options?: QueryOptions, callback?: Callback<number>): QueryWithHelpers<number, HydratedDocument<T, TMethodsAndOverrides, TVirtuals>, TQueryHelpers, T>;
973
984
 
974
985
  /**
975
- * Returns true if at least one document exists in the database that matches
976
- * the given `filter`, and false otherwise.
986
+ * Returns a document with its `_id` if at least one document exists in the database that matches
987
+ * the given `filter`, and `null` otherwise.
977
988
  */
978
- exists(filter: FilterQuery<T>): Promise<boolean>;
979
- exists(filter: FilterQuery<T>, callback: Callback<boolean>): void;
989
+ exists(filter: FilterQuery<T>): QueryWithHelpers<Pick<Document<T>, '_id'> | null, HydratedDocument<T, TMethodsAndOverrides, TVirtuals>, TQueryHelpers, T>;
990
+ exists(filter: FilterQuery<T>, callback: Callback<Pick<Document<T>, '_id'> | null>): QueryWithHelpers<Pick<Document<T>, '_id'> | null, HydratedDocument<T, TMethodsAndOverrides, TVirtuals>, TQueryHelpers, T>;
980
991
 
981
992
  /** Creates a `find` query: gets a list of documents that match `filter`. */
982
- find(callback?: Callback<HydratedDocument<T, TMethods, TVirtuals>[]>): QueryWithHelpers<Array<HydratedDocument<T, TMethods, TVirtuals>>, HydratedDocument<T, TMethods, TVirtuals>, TQueryHelpers, T>;
983
- find(filter: FilterQuery<T>, callback?: Callback<T[]>): QueryWithHelpers<Array<HydratedDocument<T, TMethods, TVirtuals>>, HydratedDocument<T, TMethods, TVirtuals>, TQueryHelpers, T>;
984
- find(filter: FilterQuery<T>, projection?: any | null, options?: QueryOptions | null, callback?: Callback<HydratedDocument<T, TMethods, TVirtuals>[]>): QueryWithHelpers<Array<HydratedDocument<T, TMethods, TVirtuals>>, HydratedDocument<T, TMethods, TVirtuals>, TQueryHelpers, T>;
993
+ find<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(callback?: Callback<ResultDoc[]>): QueryWithHelpers<Array<ResultDoc>, ResultDoc, TQueryHelpers, T>;
994
+ find<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(filter: FilterQuery<T>, callback?: Callback<ResultDoc[]>): QueryWithHelpers<Array<ResultDoc>, ResultDoc, TQueryHelpers, T>;
995
+ find<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(filter: FilterQuery<T>, projection?: any | null, options?: QueryOptions | null, callback?: Callback<ResultDoc[]>): QueryWithHelpers<Array<ResultDoc>, ResultDoc, TQueryHelpers, T>;
985
996
 
986
997
  /** Creates a `findByIdAndDelete` query, filtering by the given `_id`. */
987
- findByIdAndDelete(id?: mongodb.ObjectId | any, options?: QueryOptions | null, callback?: (err: CallbackError, doc: HydratedDocument<T, TMethods, TVirtuals> | null, res: any) => void): QueryWithHelpers<HydratedDocument<T, TMethods, TVirtuals> | null, HydratedDocument<T, TMethods, TVirtuals>, TQueryHelpers, T>;
998
+ findByIdAndDelete<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(id?: mongodb.ObjectId | any, options?: QueryOptions | null, callback?: (err: CallbackError, doc: ResultDoc | null, res: any) => void): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, T>;
988
999
 
989
1000
  /** Creates a `findByIdAndRemove` query, filtering by the given `_id`. */
990
- findByIdAndRemove(id?: mongodb.ObjectId | any, options?: QueryOptions | null, callback?: (err: CallbackError, doc: HydratedDocument<T, TMethods, TVirtuals> | null, res: any) => void): QueryWithHelpers<HydratedDocument<T, TMethods, TVirtuals> | null, HydratedDocument<T, TMethods, TVirtuals>, TQueryHelpers, T>;
1001
+ findByIdAndRemove<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(id?: mongodb.ObjectId | any, options?: QueryOptions | null, callback?: (err: CallbackError, doc: ResultDoc | null, res: any) => void): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, T>;
991
1002
 
992
1003
  /** Creates a `findOneAndUpdate` query, filtering by the given `_id`. */
993
- findByIdAndUpdate(id: mongodb.ObjectId | any, update: UpdateQuery<T>, options: QueryOptions & { rawResult: true }, callback?: (err: CallbackError, doc: any, res: any) => void): QueryWithHelpers<mongodb.ModifyResult<HydratedDocument<T, TMethods, TVirtuals>>, HydratedDocument<T, TMethods, TVirtuals>, TQueryHelpers, T>;
994
- findByIdAndUpdate(id: mongodb.ObjectId | any, update: UpdateQuery<T>, options: QueryOptions & { upsert: true } & ReturnsNewDoc, callback?: (err: CallbackError, doc: HydratedDocument<T, TMethods, TVirtuals>, res: any) => void): QueryWithHelpers<HydratedDocument<T, TMethods, TVirtuals>, HydratedDocument<T, TMethods, TVirtuals>, TQueryHelpers, T>;
995
- findByIdAndUpdate(id?: mongodb.ObjectId | any, update?: UpdateQuery<T>, options?: QueryOptions | null, callback?: (err: CallbackError, doc: HydratedDocument<T, TMethods, TVirtuals> | null, res: any) => void): QueryWithHelpers<HydratedDocument<T, TMethods, TVirtuals> | null, HydratedDocument<T, TMethods, TVirtuals>, TQueryHelpers, T>;
996
- findByIdAndUpdate(id: mongodb.ObjectId | any, update: UpdateQuery<T>, callback: (err: CallbackError, doc: HydratedDocument<T, TMethods, TVirtuals> | null, res: any) => void): QueryWithHelpers<HydratedDocument<T, TMethods, TVirtuals> | null, HydratedDocument<T, TMethods, TVirtuals>, TQueryHelpers, T>;
1004
+ findByIdAndUpdate<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(id: mongodb.ObjectId | any, update: UpdateQuery<T>, options: QueryOptions & { rawResult: true }, callback?: (err: CallbackError, doc: any, res: any) => void): QueryWithHelpers<ModifyResult<ResultDoc>, ResultDoc, TQueryHelpers, T>;
1005
+ findByIdAndUpdate<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(id: mongodb.ObjectId | any, update: UpdateQuery<T>, options: QueryOptions & { upsert: true } & ReturnsNewDoc, callback?: (err: CallbackError, doc: ResultDoc, res: any) => void): QueryWithHelpers<ResultDoc, ResultDoc, TQueryHelpers, T>;
1006
+ findByIdAndUpdate<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(id?: mongodb.ObjectId | any, update?: UpdateQuery<T>, options?: QueryOptions | null, callback?: (err: CallbackError, doc: ResultDoc | null, res: any) => void): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, T>;
1007
+ findByIdAndUpdate<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(id: mongodb.ObjectId | any, update: UpdateQuery<T>, callback: (err: CallbackError, doc: ResultDoc | null, res: any) => void): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, T>;
997
1008
 
998
1009
  /** Creates a `findOneAndDelete` query: atomically finds the given document, deletes it, and returns the document as it was before deletion. */
999
- findOneAndDelete(filter?: FilterQuery<T>, options?: QueryOptions | null, callback?: (err: CallbackError, doc: HydratedDocument<T, TMethods, TVirtuals> | null, res: any) => void): QueryWithHelpers<HydratedDocument<T, TMethods, TVirtuals> | null, HydratedDocument<T, TMethods, TVirtuals>, TQueryHelpers, T>;
1010
+ findOneAndDelete<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(filter?: FilterQuery<T>, options?: QueryOptions | null, callback?: (err: CallbackError, doc: ResultDoc | null, res: any) => void): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, T>;
1000
1011
 
1001
1012
  /** Creates a `findOneAndRemove` query: atomically finds the given document and deletes it. */
1002
- findOneAndRemove(filter?: FilterQuery<T>, options?: QueryOptions | null, callback?: (err: CallbackError, doc: HydratedDocument<T, TMethods, TVirtuals> | null, res: any) => void): QueryWithHelpers<HydratedDocument<T, TMethods, TVirtuals> | null, HydratedDocument<T, TMethods, TVirtuals>, TQueryHelpers, T>;
1013
+ findOneAndRemove<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(filter?: FilterQuery<T>, options?: QueryOptions | null, callback?: (err: CallbackError, doc: ResultDoc | null, res: any) => void): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, T>;
1003
1014
 
1004
1015
  /** Creates a `findOneAndReplace` query: atomically finds the given document and replaces it with `replacement`. */
1005
- findOneAndReplace(filter: FilterQuery<T>, replacement: T | AnyObject, options: QueryOptions & { upsert: true } & ReturnsNewDoc, callback?: (err: CallbackError, doc: HydratedDocument<T, TMethods, TVirtuals>, res: any) => void): QueryWithHelpers<HydratedDocument<T, TMethods, TVirtuals>, HydratedDocument<T, TMethods, TVirtuals>, TQueryHelpers, T>;
1006
- findOneAndReplace(filter?: FilterQuery<T>, replacement?: T | AnyObject, options?: QueryOptions | null, callback?: (err: CallbackError, doc: HydratedDocument<T, TMethods, TVirtuals> | null, res: any) => void): QueryWithHelpers<HydratedDocument<T, TMethods, TVirtuals> | null, HydratedDocument<T, TMethods, TVirtuals>, TQueryHelpers, T>;
1016
+ findOneAndReplace<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(filter: FilterQuery<T>, replacement: T | AnyObject, options: QueryOptions & { upsert: true } & ReturnsNewDoc, callback?: (err: CallbackError, doc: ResultDoc, res: any) => void): QueryWithHelpers<ResultDoc, ResultDoc, TQueryHelpers, T>;
1017
+ findOneAndReplace<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(filter?: FilterQuery<T>, replacement?: T | AnyObject, options?: QueryOptions | null, callback?: (err: CallbackError, doc: ResultDoc | null, res: any) => void): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, T>;
1007
1018
 
1008
1019
  /** Creates a `findOneAndUpdate` query: atomically find the first document that matches `filter` and apply `update`. */
1009
- findOneAndUpdate(filter: FilterQuery<T>, update: UpdateQuery<T>, options: QueryOptions & { rawResult: true }, callback?: (err: CallbackError, doc: any, res: any) => void): QueryWithHelpers<mongodb.ModifyResult<HydratedDocument<T, TMethods, TVirtuals>>, HydratedDocument<T, TMethods, TVirtuals>, TQueryHelpers, T>;
1010
- findOneAndUpdate(filter: FilterQuery<T>, update: UpdateQuery<T>, options: QueryOptions & { upsert: true } & ReturnsNewDoc, callback?: (err: CallbackError, doc: HydratedDocument<T, TMethods, TVirtuals>, res: any) => void): QueryWithHelpers<HydratedDocument<T, TMethods, TVirtuals>, HydratedDocument<T, TMethods, TVirtuals>, TQueryHelpers, T>;
1011
- findOneAndUpdate(filter?: FilterQuery<T>, update?: UpdateQuery<T>, options?: QueryOptions | null, callback?: (err: CallbackError, doc: T | null, res: any) => void): QueryWithHelpers<HydratedDocument<T, TMethods, TVirtuals> | null, HydratedDocument<T, TMethods, TVirtuals>, TQueryHelpers, T>;
1020
+ findOneAndUpdate<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(filter: FilterQuery<T>, update: UpdateQuery<T>, options: QueryOptions & { rawResult: true }, callback?: (err: CallbackError, doc: any, res: any) => void): QueryWithHelpers<ModifyResult<ResultDoc>, ResultDoc, TQueryHelpers, T>;
1021
+ findOneAndUpdate<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(filter: FilterQuery<T>, update: UpdateQuery<T>, options: QueryOptions & { upsert: true } & ReturnsNewDoc, callback?: (err: CallbackError, doc: ResultDoc, res: any) => void): QueryWithHelpers<ResultDoc, ResultDoc, TQueryHelpers, T>;
1022
+ findOneAndUpdate<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(filter?: FilterQuery<T>, update?: UpdateQuery<T>, options?: QueryOptions | null, callback?: (err: CallbackError, doc: T | null, res: any) => void): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, T>;
1012
1023
 
1013
- geoSearch(filter?: FilterQuery<T>, options?: GeoSearchOptions, callback?: Callback<Array<HydratedDocument<T, TMethods, TVirtuals>>>): QueryWithHelpers<Array<HydratedDocument<T, TMethods, TVirtuals>>, HydratedDocument<T, TMethods, TVirtuals>, TQueryHelpers, T>;
1024
+ geoSearch<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(filter?: FilterQuery<T>, options?: GeoSearchOptions, callback?: Callback<Array<ResultDoc>>): QueryWithHelpers<Array<ResultDoc>, ResultDoc, TQueryHelpers, T>;
1014
1025
 
1015
1026
  /** Executes a mapReduce command. */
1016
1027
  mapReduce<Key, Value>(
@@ -1018,11 +1029,11 @@ declare module 'mongoose' {
1018
1029
  callback?: Callback
1019
1030
  ): Promise<any>;
1020
1031
 
1021
- remove(filter?: any, callback?: CallbackWithoutResult): QueryWithHelpers<any, HydratedDocument<T, TMethods, TVirtuals>, TQueryHelpers, T>;
1032
+ remove<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(filter?: any, callback?: CallbackWithoutResult): QueryWithHelpers<any, ResultDoc, TQueryHelpers, T>;
1022
1033
 
1023
1034
  /** Creates a `replaceOne` query: finds the first document that matches `filter` and replaces it with `replacement`. */
1024
- replaceOne(filter?: FilterQuery<T>, replacement?: T | AnyObject, options?: QueryOptions | null, callback?: Callback): QueryWithHelpers<any, HydratedDocument<T, TMethods, TVirtuals>, TQueryHelpers, T>;
1025
- replaceOne(filter?: FilterQuery<T>, replacement?: T | AnyObject, options?: QueryOptions | null, callback?: Callback): QueryWithHelpers<any, HydratedDocument<T, TMethods, TVirtuals>, TQueryHelpers, T>;
1035
+ replaceOne<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(filter?: FilterQuery<T>, replacement?: T | AnyObject, options?: QueryOptions | null, callback?: Callback): QueryWithHelpers<any, ResultDoc, TQueryHelpers, T>;
1036
+ replaceOne<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(filter?: FilterQuery<T>, replacement?: T | AnyObject, options?: QueryOptions | null, callback?: Callback): QueryWithHelpers<any, ResultDoc, TQueryHelpers, T>;
1026
1037
 
1027
1038
  /** Schema the model uses. */
1028
1039
  schema: Schema<T>;
@@ -1031,18 +1042,18 @@ declare module 'mongoose' {
1031
1042
  * @deprecated use `updateOne` or `updateMany` instead.
1032
1043
  * Creates a `update` query: updates one or many documents that match `filter` with `update`, based on the `multi` option.
1033
1044
  */
1034
- update(filter?: FilterQuery<T>, update?: UpdateQuery<T> | UpdateWithAggregationPipeline, options?: QueryOptions | null, callback?: Callback): QueryWithHelpers<UpdateWriteOpResult, HydratedDocument<T, TMethods, TVirtuals>, TQueryHelpers, T>;
1045
+ update<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(filter?: FilterQuery<T>, update?: UpdateQuery<T> | UpdateWithAggregationPipeline, options?: QueryOptions | null, callback?: Callback): QueryWithHelpers<UpdateWriteOpResult, ResultDoc, TQueryHelpers, T>;
1035
1046
 
1036
1047
  /** Creates a `updateMany` query: updates all documents that match `filter` with `update`. */
1037
- updateMany(filter?: FilterQuery<T>, update?: UpdateQuery<T> | UpdateWithAggregationPipeline, options?: QueryOptions | null, callback?: Callback): QueryWithHelpers<UpdateWriteOpResult, HydratedDocument<T, TMethods, TVirtuals>, TQueryHelpers, T>;
1048
+ updateMany<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(filter?: FilterQuery<T>, update?: UpdateQuery<T> | UpdateWithAggregationPipeline, options?: QueryOptions | null, callback?: Callback): QueryWithHelpers<UpdateWriteOpResult, ResultDoc, TQueryHelpers, T>;
1038
1049
 
1039
1050
  /** Creates a `updateOne` query: updates the first document that matches `filter` with `update`. */
1040
- updateOne(filter?: FilterQuery<T>, update?: UpdateQuery<T> | UpdateWithAggregationPipeline, options?: QueryOptions | null, callback?: Callback): QueryWithHelpers<UpdateWriteOpResult, HydratedDocument<T, TMethods, TVirtuals>, TQueryHelpers, T>;
1051
+ updateOne<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(filter?: FilterQuery<T>, update?: UpdateQuery<T> | UpdateWithAggregationPipeline, options?: QueryOptions | null, callback?: Callback): QueryWithHelpers<UpdateWriteOpResult, ResultDoc, TQueryHelpers, T>;
1041
1052
 
1042
1053
  /** Creates a Query, applies the passed conditions, and returns the Query. */
1043
- where(path: string, val?: any): QueryWithHelpers<Array<HydratedDocument<T, TMethods, TVirtuals>>, HydratedDocument<T, TMethods, TVirtuals>, TQueryHelpers, T>;
1044
- where(obj: object): QueryWithHelpers<Array<HydratedDocument<T, TMethods, TVirtuals>>, HydratedDocument<T, TMethods, TVirtuals>, TQueryHelpers, T>;
1045
- where(): QueryWithHelpers<Array<HydratedDocument<T, TMethods, TVirtuals>>, HydratedDocument<T, TMethods, TVirtuals>, TQueryHelpers, T>;
1054
+ where<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(path: string, val?: any): QueryWithHelpers<Array<ResultDoc>, ResultDoc, TQueryHelpers, T>;
1055
+ where<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(obj: object): QueryWithHelpers<Array<ResultDoc>, ResultDoc, TQueryHelpers, T>;
1056
+ where<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(): QueryWithHelpers<Array<ResultDoc>, ResultDoc, TQueryHelpers, T>;
1046
1057
  }
1047
1058
 
1048
1059
  type UpdateWriteOpResult = mongodb.UpdateResult;
@@ -1408,6 +1419,7 @@ declare module 'mongoose' {
1408
1419
  type StringSchemaDefinition = typeof String | 'string' | 'String' | typeof Schema.Types.String;
1409
1420
  type BooleanSchemaDefinition = typeof Boolean | 'boolean' | 'Boolean' | typeof Schema.Types.Boolean;
1410
1421
  type DateSchemaDefinition = typeof NativeDate | 'date' | 'Date' | typeof Schema.Types.Date;
1422
+ type ObjectIdSchemaDefinition = 'ObjectId' | 'ObjectID' | typeof Schema.Types.ObjectId;
1411
1423
 
1412
1424
  type SchemaDefinitionWithBuiltInClass<T> = T extends number
1413
1425
  ? NumberSchemaDefinition
@@ -1505,6 +1517,11 @@ declare module 'mongoose' {
1505
1517
  * optimistic concurrency.
1506
1518
  */
1507
1519
  optimisticConcurrency?: boolean;
1520
+ /**
1521
+ * If `plugin()` called with tags, Mongoose will only apply plugins to schemas that have
1522
+ * a matching tag in `pluginTags`
1523
+ */
1524
+ pluginTags?: string[];
1508
1525
  /**
1509
1526
  * Allows setting query#read options at the schema level, providing us a way to apply default ReadPreferences
1510
1527
  * to all queries derived from a model.
@@ -1603,6 +1620,8 @@ declare module 'mongoose' {
1603
1620
  T extends NativeDate ? DateSchemaDefinition :
1604
1621
  T extends Map<any, any> ? SchemaDefinition<typeof Map> :
1605
1622
  T extends Buffer ? SchemaDefinition<typeof Buffer> :
1623
+ T extends Types.ObjectId ? ObjectIdSchemaDefinition :
1624
+ T extends Types.ObjectId[] ? AnyArray<ObjectIdSchemaDefinition> | AnyArray<SchemaTypeOptions<ObjectId>> :
1606
1625
  T extends object[] ? (AnyArray<Schema<any, any, any>> | AnyArray<SchemaDefinition<Unpacked<T>>> | AnyArray<SchemaTypeOptions<Unpacked<T>>>) :
1607
1626
  T extends string[] ? AnyArray<StringSchemaDefinition> | AnyArray<SchemaTypeOptions<string>> :
1608
1627
  T extends number[] ? AnyArray<NumberSchemaDefinition> | AnyArray<SchemaTypeOptions<number>> :
@@ -1808,6 +1827,8 @@ declare module 'mongoose' {
1808
1827
  validator: ValidateFn<T> | LegacyAsyncValidateFn<T> | AsyncValidateFn<T>;
1809
1828
  }
1810
1829
 
1830
+ type InferId<T> = T extends { _id?: any } ? T['_id'] : Types.ObjectId;
1831
+
1811
1832
  interface VirtualTypeOptions {
1812
1833
  /** If `ref` is not nullish, this becomes a populated virtual. */
1813
1834
  ref?: string | Function;
@@ -2076,17 +2097,17 @@ declare module 'mongoose' {
2076
2097
 
2077
2098
  class Decimal128 extends mongodb.Decimal128 { }
2078
2099
 
2079
- class DocumentArray<T> extends Types.Array<T> {
2100
+ class DocumentArray<T> extends Types.Array<T extends Types.Subdocument ? T : Types.Subdocument<InferId<T>> & T> {
2080
2101
  /** DocumentArray constructor */
2081
2102
  constructor(values: any[]);
2082
2103
 
2083
2104
  isMongooseDocumentArray: true;
2084
2105
 
2085
2106
  /** Creates a subdocument casted to this schema. */
2086
- create(obj: any): T;
2107
+ create(obj: any): T extends Types.Subdocument ? T : Types.Subdocument<InferId<T>> & T;
2087
2108
 
2088
2109
  /** Searches array items for the first document with a matching _id. */
2089
- id(id: any): T | null;
2110
+ id(id: any): (T extends Types.Subdocument ? T : Types.Subdocument<InferId<T>> & T) | null;
2090
2111
 
2091
2112
  push(...args: (AnyKeys<T> & AnyObject)[]): number;
2092
2113
  }
@@ -2126,6 +2147,8 @@ declare module 'mongoose' {
2126
2147
  type UnpackedIntersection<T, U> = T extends (infer V)[] ? (V & U)[] : T & U;
2127
2148
  type UnpackedIntersectionWithNull<T, U> = T extends null ? UnpackedIntersection<T, U> | null : UnpackedIntersection<T, U>;
2128
2149
 
2150
+ type ProjectionFields<DocType> = {[Key in keyof Omit<LeanDocument<DocType>, '__v'>]?: any} & Record<string, any>;
2151
+
2129
2152
  class Query<ResultType, DocType, THelpers = {}, RawDocType = DocType> {
2130
2153
  _mongooseOptions: MongooseQueryOptions;
2131
2154
 
@@ -2269,9 +2292,24 @@ declare module 'mongoose' {
2269
2292
  findOneAndRemove(filter?: FilterQuery<DocType>, options?: QueryOptions | null, callback?: (err: CallbackError, doc: DocType | null, res: any) => void): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType>;
2270
2293
 
2271
2294
  /** Creates a `findOneAndUpdate` query: atomically find the first document that matches `filter` and apply `update`. */
2272
- findOneAndUpdate(filter: FilterQuery<DocType>, update: UpdateQuery<DocType>, options: QueryOptions & { rawResult: true }, callback?: (err: CallbackError, doc: DocType | null, res: mongodb.ModifyResult<DocType>) => void): QueryWithHelpers<mongodb.ModifyResult<DocType>, DocType, THelpers, RawDocType>;
2273
- findOneAndUpdate(filter: FilterQuery<DocType>, update: UpdateQuery<DocType>, options: QueryOptions & { upsert: true } & ReturnsNewDoc, callback?: (err: CallbackError, doc: DocType, res: mongodb.ModifyResult<DocType>) => void): QueryWithHelpers<DocType, DocType, THelpers, RawDocType>;
2274
- findOneAndUpdate(filter?: FilterQuery<DocType>, update?: UpdateQuery<DocType>, options?: QueryOptions | null, callback?: (err: CallbackError, doc: DocType | null, res: mongodb.ModifyResult<DocType>) => void): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType>;
2295
+ findOneAndUpdate(
2296
+ filter: FilterQuery<DocType>,
2297
+ update: UpdateQuery<DocType>,
2298
+ options: QueryOptions & { rawResult: true },
2299
+ callback?: (err: CallbackError, doc: DocType | null, res: ModifyResult<DocType>) => void
2300
+ ): QueryWithHelpers<ModifyResult<DocType>, DocType, THelpers, RawDocType>;
2301
+ findOneAndUpdate(
2302
+ filter: FilterQuery<DocType>,
2303
+ update: UpdateQuery<DocType>,
2304
+ options: QueryOptions & { upsert: true } & ReturnsNewDoc,
2305
+ callback?: (err: CallbackError, doc: DocType, res: ModifyResult<DocType>) => void
2306
+ ): QueryWithHelpers<DocType, DocType, THelpers, RawDocType>;
2307
+ findOneAndUpdate(
2308
+ filter?: FilterQuery<DocType>,
2309
+ update?: UpdateQuery<DocType>,
2310
+ options?: QueryOptions | null,
2311
+ callback?: (err: CallbackError, doc: DocType | null, res: ModifyResult<DocType>) => void
2312
+ ): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType>;
2275
2313
 
2276
2314
  /** Creates a `findByIdAndDelete` query, filtering by the given `_id`. */
2277
2315
  findByIdAndDelete(id?: mongodb.ObjectId | any, options?: QueryOptions | null, callback?: (err: CallbackError, doc: DocType | null, res: any) => void): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType>;
@@ -2412,7 +2450,9 @@ declare module 'mongoose' {
2412
2450
  populate<Paths = {}>(options: PopulateOptions | Array<PopulateOptions>): QueryWithHelpers<UnpackedIntersectionWithNull<ResultType, Paths>, DocType, THelpers, RawDocType>;
2413
2451
 
2414
2452
  /** Get/set the current projection (AKA fields). Pass `null` to remove the current projection. */
2415
- projection(fields?: any | null): this;
2453
+ projection(): ProjectionFields<DocType> | null;
2454
+ projection(fields: null): null;
2455
+ projection(fields?: ProjectionFields<DocType> | string): ProjectionFields<DocType>;
2416
2456
 
2417
2457
  /** Determines the MongoDB nodes from which to read. */
2418
2458
  read(pref: string | mongodb.ReadPreferenceMode, tags?: any[]): this;
@@ -3262,9 +3302,15 @@ declare module 'mongoose' {
3262
3302
  /** String representation of what type this is, like 'ObjectID' or 'Number' */
3263
3303
  instance: string;
3264
3304
 
3305
+ /** True if this SchemaType has a required validator. False otherwise. */
3306
+ isRequired?: boolean;
3307
+
3265
3308
  /** The options this SchemaType was instantiated with */
3266
3309
  options: AnyObject;
3267
3310
 
3311
+ /** The path to this SchemaType in a Schema. */
3312
+ path: string;
3313
+
3268
3314
  /**
3269
3315
  * Set the model that this path refers to. This is the option that [populate](https://mongoosejs.com/docs/populate.html)
3270
3316
  * looks at to determine the foreign collection it should query.
@@ -3298,11 +3344,19 @@ declare module 'mongoose' {
3298
3344
  /** Declares an unique index. */
3299
3345
  unique(bool: boolean): this;
3300
3346
 
3347
+ /** The validators that Mongoose should run to validate properties at this SchemaType's path. */
3348
+ validators: { message?: string; type?: string; validator?: Function }[];
3349
+
3301
3350
  /** Adds validator(s) for this document path. */
3302
3351
  validate(obj: RegExp | Function | any, errorMsg?: string,
3303
3352
  type?: string): this;
3304
3353
  }
3305
3354
 
3355
+ export interface SyncIndexesOptions extends mongodb.CreateIndexesOptions {
3356
+ continueOnError?: boolean
3357
+ }
3358
+ export type ConnectionSyncIndexesResult = Record<string, OneCollectionSyncIndexesResult>;
3359
+ type OneCollectionSyncIndexesResult = Array<string> & mongodb.MongoServerError;
3306
3360
  type Callback<T = any> = (error: CallbackError, result: T) => void;
3307
3361
 
3308
3362
  type CallbackWithoutResult = (error: CallbackError) => void;
@@ -3333,6 +3387,12 @@ declare module 'mongoose' {
3333
3387
 
3334
3388
  constructor(type: string, value: any, path: string, reason?: NativeError, schemaType?: SchemaType);
3335
3389
  }
3390
+ export class SyncIndexesError extends Error {
3391
+ name: 'SyncIndexesError';
3392
+ errors?: Record<string, mongodb.MongoServerError>;
3393
+
3394
+ constructor(type: string, value: any, path: string, reason?: NativeError, schemaType?: SchemaType);
3395
+ }
3336
3396
 
3337
3397
  export class DisconnectedError extends Error {
3338
3398
  name: 'DisconnectedError';