Class Document<T, TQueryHelpers, DocType>

Generic types for Document:

  • T - the type of _id
  • TQueryHelpers - Object with any helpers that should be mixed into the Query type
  • DocType - the type of the actual Document created

Type Parameters

  • T = any

  • TQueryHelpers = any

  • DocType = any

Hierarchy

Constructors

  • Type Parameters

    • T = any

    • TQueryHelpers = any

    • DocType = any

    Parameters

    • Optional doc: any
      Optional

    Returns mongoose.Document<T, TQueryHelpers, DocType>

Properties

$locals: Record<string, unknown>

Empty object that you can use for storing properties on the document. This is handy for passing data to middleware without conflicting with Mongoose internals.

$op: "validate" | "save" | "remove"

A string containing the current operation that Mongoose is executing on this document. Can be null, 'save', 'validate', or 'remove'.

$where: Record<string, unknown>

Set this property to add additional query filters when Mongoose saves this document and isNew is false.

__v?: any

This documents __v.

_id?: T

This documents _id.

baseModelName?: string

If this is a discriminator model, baseModelName is the name of the base model.

Collection the model uses.

Connection the model uses.

Returns the current validation errors.

id?: any

The string version of this documents _id.

isNew: boolean

Boolean flag specifying if the document is new.

schema: Schema<any, mongoose.Model<any, any, any, any, any, any>, {}, {}, {}, {}, DefaultSchemaOptions, {}, mongoose.Document<unknown, {}, FlatRecord<{}>> & FlatRecord<{}> & Required<{
    _id: unknown;
}>>

The document's schema.

Type declaration

    Type declaration

      Type declaration

        Type declaration

          Type declaration

            Methods

            • Assert that a given path or paths is populated. Throws an error if not populated.

              Type Parameters

              • Paths = {}

              Parameters

              • path: string | string[]
              • Optional values: Partial<Paths>
                Optional

              Returns Omit<mongoose.Document<T, TQueryHelpers, DocType>, keyof Paths> & Paths

            • Returns a deep clone of this document

              Returns mongoose.Document<T, TQueryHelpers, DocType>

            • Returns mongoose.Document<any, any, any>[]

            • Returns an array of all populated documents associated with the query

              Returns mongoose.Document<any, any, any>[]

            • Don't run validation on this path or persist changes to this path.

              Parameters

              • path: string

              Returns void

            • Increments the numeric value at path by the given val. When you call save() on this document, Mongoose will send a $inc as opposed to a $set.

              Parameters

              • path: string | string[]
              • Optional val: number
                Optional

              Returns mongoose.Document<T, TQueryHelpers, DocType>

            • Checks if a path is set to its default.

              Parameters

              • path: string

              Returns boolean

            • Getter/setter, determines whether the document was removed or not.

              Parameters

              • Optional val: boolean
                Optional

              Returns boolean

            • Returns true if the given path is nullish or only contains empty objects. Useful for determining whether this subdoc will get stripped out by the minimize option.

              Parameters

              • path: string

              Returns boolean

            • Checks if a path is invalid

              Parameters

              • path: string

              Returns boolean

            • Marks a path as valid, removing existing validation errors.

              Parameters

              • path: string

              Returns void

            • Returns the model with the given name on this document's associated connection.

              Type Parameters

              Parameters

              • name: string

              Returns ModelType

            • Type Parameters

              Returns ModelType

            • If this document is a subdocument or populated document, returns the document's parent. Returns undefined otherwise.

              Returns mongoose.Document<any, any, any>

            • Getter/setter around the session associated with this document. Used to automatically set session if you save() a doc that you got from a query with an associated session.

              Parameters

              Returns mongoose.mongo.ClientSession

            • Alias for set(), used internally to avoid conflicts

              Parameters

              • path: string | Record<string, any>
              • val: any
              • type: any
              • Optional options: DocumentSetOptions
                Optional

              Returns mongoose.Document<T, TQueryHelpers, DocType>

            • Parameters

              • path: string | Record<string, any>
              • val: any
              • Optional options: DocumentSetOptions
                Optional

              Returns mongoose.Document<T, TQueryHelpers, DocType>

            • Parameters

              • value: string | Record<string, any>

              Returns mongoose.Document<T, TQueryHelpers, DocType>

            • Removes this document from the db.

              Parameters

              Returns Promise<mongoose.Document<T, TQueryHelpers, DocType>>

            • Takes a populated field and returns it to its unpopulated state. If called with no arguments, then all populated fields are returned to their unpopulated state.

              Parameters

              • Optional path: string | string[]
                Optional

              Returns mongoose.Document<T, TQueryHelpers, DocType>

            • Returns the list of paths that have been directly modified. A direct modified path is a path that you explicitly set, whether via doc.foo = 'bar', Object.assign(doc, { foo: 'bar' }), or doc.set('foo', 'bar').

              Returns string[]

            • Returns true if this document is equal to another document.

              Documents are considered equal when they have matching _ids, unless neither document has an _id, in which case this function falls back to using deepEqual().

              Parameters

              Returns boolean

            • Returns the value of a path.

              Type Parameters

              • T extends string | number | symbol

              Parameters

              • path: T
              • Optional type: any
                Optional
              • Optional options: any
                Optional

              Returns DocType[T]

            • Parameters

              • path: string
              • Optional type: any
                Optional
              • Optional options: any
                Optional

              Returns any

            • Returns the changes that happened to the document in the format that will be sent to MongoDB.

              Returns UpdateQuery<mongoose.Document<T, TQueryHelpers, DocType>>

            • Signal that we desire an increment of this documents version.

              Returns mongoose.Document<T, TQueryHelpers, DocType>

            • Initializes the document without setters or marking anything modified. Called internally after a document is returned from mongodb. Normally, you do not need to call this function on your own.

              Parameters

              Returns mongoose.Document<T, TQueryHelpers, DocType>

            • Marks a path as invalid, causing validation to fail.

              Type Parameters

              • T extends string | number | symbol

              Parameters

              • path: T
              • errorMsg: string | NativeError
              • Optional value: any
                Optional
              • Optional kind: string
                Optional

              Returns NativeError

            • Parameters

              • path: string
              • errorMsg: string | NativeError
              • Optional value: any
                Optional
              • Optional kind: string
                Optional

              Returns NativeError

            • Returns true if path was directly set and modified, else false.

              Type Parameters

              • T extends string | number | symbol

              Parameters

              • path: T | T[]

              Returns boolean

            • Parameters

              • path: string | string[]

              Returns boolean

            • Checks if path was explicitly selected. If no projection, always returns true.

              Type Parameters

              • T extends string | number | symbol

              Parameters

              • path: T

              Returns boolean

            • Parameters

              • path: string

              Returns boolean

            • Checks if path is in the init state, that is, it was set by Document#init() and not modified since.

              Type Parameters

              • T extends string | number | symbol

              Parameters

              • path: T

              Returns boolean

            • Parameters

              • path: string

              Returns boolean

            • Returns true if any of the given paths are modified, else false. If no arguments, returns true if any path in this document is modified.

              Type Parameters

              • T extends string | number | symbol

              Parameters

              • Optional path: T | T[]
                Optional
              • Optional options: {
                    ignoreAtomics?: boolean;
                }
                Optional
                • Optional ignoreAtomics?: boolean

              Returns boolean

            • Parameters

              • Optional path: string | string[]
                Optional
              • Optional options: {
                    ignoreAtomics?: boolean;
                }
                Optional
                • Optional ignoreAtomics?: boolean

              Returns boolean

            • Checks if path was selected in the source query which initialized this document.

              Type Parameters

              • T extends string | number | symbol

              Parameters

              • path: T

              Returns boolean

            • Parameters

              • path: string

              Returns boolean

            • Marks the path as having pending changes to write to the db.

              Type Parameters

              • T extends string | number | symbol

              Parameters

              • path: T
              • Optional scope: any
                Optional

              Returns void

            • Parameters

              • path: string
              • Optional scope: any
                Optional

              Returns void

            • Returns the model with the given name on this document's associated connection.

              Type Parameters

              Parameters

              • name: string

              Returns ModelType

            • Type Parameters

              Returns ModelType

            • Returns the list of paths that have been modified.

              Parameters

              • Optional options: {
                    includeChildren?: boolean;
                }
                Optional
                • Optional includeChildren?: boolean

              Returns string[]

            • Overwrite all values in this document with the values of obj, except for immutable properties. Behaves similarly to set(), except for it unsets all properties that aren't in obj.

              Parameters

              Returns mongoose.Document<T, TQueryHelpers, DocType>

            • Populates document references.

              Type Parameters

              • Paths = {}

              Parameters

              Returns Promise<MergeType<mongoose.Document<T, TQueryHelpers, DocType>, Paths>>

            • Type Parameters

              • Paths = {}

              Parameters

              Returns Promise<MergeType<mongoose.Document<T, TQueryHelpers, DocType>, Paths>>

            • Gets _id(s) used during population of the given path. If the path was not populated, returns undefined.

              Parameters

              • path: string

              Returns any

            • Sends a replaceOne command with this document _id as the query selector.

              Parameters

              Returns Query<any, mongoose.Document<T, TQueryHelpers, DocType>, {}, mongoose.Document<T, TQueryHelpers, DocType>, "find">

            • Saves this document by inserting a new document into the database if document.isNew is true, or sends an updateOne operation with just the modified paths if isNew is false.

              Parameters

              Returns Promise<mongoose.Document<T, TQueryHelpers, DocType>>

            • Sets the value of a path, or many paths.

              Type Parameters

              • T extends string | number | symbol

              Parameters

              Returns mongoose.Document<T, TQueryHelpers, DocType>

            • Parameters

              • path: string | Record<string, any>
              • val: any
              • type: any
              • Optional options: DocumentSetOptions
                Optional

              Returns mongoose.Document<T, TQueryHelpers, DocType>

            • Parameters

              • path: string | Record<string, any>
              • val: any
              • Optional options: DocumentSetOptions
                Optional

              Returns mongoose.Document<T, TQueryHelpers, DocType>

            • Parameters

              • value: string | Record<string, any>

              Returns mongoose.Document<T, TQueryHelpers, DocType>

            • The return value of this method is used in calls to JSON.stringify(doc).

              Type Parameters

              Parameters

              • Optional options: ToObjectOptions<mongoose.Document<unknown, {}, unknown> & Required<{
                    _id: unknown;
                }>> & {
                    flattenMaps?: true;
                }
                Optional

              Returns FlattenMaps<T>

            • Type Parameters

              Parameters

              Returns T

            • Converts this document into a plain-old JavaScript object (POJO).

              Type Parameters

              Parameters

              Returns Require_id<T>

            • Clears the modified state on the specified path.

              Type Parameters

              • T extends string | number | symbol

              Parameters

              • path: T

              Returns void

            • Parameters

              • path: string

              Returns void

            • Executes registered validation rules for this document.

              Type Parameters

              • T extends string | number | symbol

              Parameters

              • Optional pathsToValidate: T | T[]
                Optional
              • Optional options: AnyObject
                Optional

              Returns Promise<void>

            • Parameters

              Returns Promise<void>

            • Parameters

              Returns Promise<void>

            • Executes registered validation rules (skipping asynchronous validators) for this document.

              Parameters

              • options: {
                    pathsToSkip?: pathsToSkip;
                    [k: string]: any;
                }

              Returns ValidationError

            • Type Parameters

              • T extends string | number | symbol

              Parameters

              • Optional pathsToValidate: T | T[]
                Optional
              • Optional options: AnyObject
                Optional

              Returns ValidationError

            • Parameters

              Returns ValidationError

            Generated using TypeDoc