Options
All
  • Public
  • Public/Protected
  • All
Menu

Interface MongooseModel

Hierarchy

  • Document
    • MongooseModel

Index

Constructors

constructor

Properties

$locals

$locals: object

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

$op: string | null

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

$where

$where: object

Additional properties to attach to the query when calling save() and isNew is false.

Optional _id

_id: any

This documents _id.

Optional baseModelName

baseModelName: undefined | string

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

collection

collection: Collection

Collection the model uses.

db

db: Connection

Connection the model uses.

Optional errors

errors: Error.ValidationError

Hash containing current validation errors.

id

id: string

The string version of this documents _id.

isNew

isNew: boolean

Boolean flag specifying if the document is new.

key

key: string

modelName

modelName: string

The name of the model

schema

schema: Schema

The document's schema.

value

value: string

Methods

$ignore

  • $ignore(path: string): void
  • Don't run validation on this path or persist changes to this path.

    Parameters

    • path: string

    Returns void

$isDefault

  • $isDefault(path: string): boolean
  • Checks if a path is set to its default.

    Parameters

    • path: string

    Returns boolean

$isDeleted

  • $isDeleted(val?: undefined | false | true): boolean
  • Getter/setter, determines whether the document was removed or not.

    Parameters

    • Optional val: undefined | false | true

    Returns boolean

$isEmpty

  • $isEmpty(path: string): 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

$isValid

  • $isValid(path: string): boolean
  • Checks if a path is invalid

    Parameters

    • path: string

    Returns boolean

$markValid

  • $markValid(path: string): void
  • Marks a path as valid, removing existing validation errors.

    Parameters

    • path: string

    Returns void

$session

  • $session(session?: ClientSession | null): ClientSession
  • 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

    • Optional session: ClientSession | null

    Returns ClientSession

$set

  • $set(path: string, val: any, options?: any): this
  • $set(path: string, val: any, type: any, options?: any): this
  • $set(value: any): this
  • Alias for set(), used internally to avoid conflicts

    Parameters

    • path: string
    • val: any
    • Optional options: any

    Returns this

  • Parameters

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

    Returns this

  • Parameters

    • value: any

    Returns this

delete

  • delete(options?: QueryOptions, cb?: undefined | ((err: CallbackError, res: any) => void)): void
  • delete(options?: QueryOptions): Query<any, this>
  • Removes this document from the db.

    Parameters

    • Optional options: QueryOptions
    • Optional cb: undefined | ((err: CallbackError, res: any) => void)

    Returns void

  • Parameters

    • Optional options: QueryOptions

    Returns Query<any, this>

deleteOne

  • deleteOne(options?: QueryOptions, cb?: undefined | ((err: CallbackError, res: any) => void)): void
  • deleteOne(options?: QueryOptions): Query<any, this>
  • Removes this document from the db.

    Parameters

    • Optional options: QueryOptions
    • Optional cb: undefined | ((err: CallbackError, res: any) => void)

    Returns void

  • Parameters

    • Optional options: QueryOptions

    Returns Query<any, this>

depopulate

  • depopulate(path: string): this
  • Takes a populated field and returns it to its unpopulated state.

    Parameters

    • path: string

    Returns this

directModifiedPaths

  • directModifiedPaths(): Array<string>
  • 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 Array<string>

equals

  • equals(doc: Document): boolean
  • 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

    • doc: Document

    Returns boolean

execPopulate

  • execPopulate(): Promise<this>
  • execPopulate(callback: (err: CallbackError, res: this) => void): void
  • Explicitly executes population and returns a promise. Useful for promises integration.

    Returns Promise<this>

  • Parameters

    • callback: (err: CallbackError, res: this) => void
        • (err: CallbackError, res: this): void
        • Parameters

          • err: CallbackError
          • res: this

          Returns void

    Returns void

get

  • get(path: string, type?: any, options?: any): any
  • Returns the value of a path.

    Parameters

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

    Returns any

getChanges

  • getChanges(): UpdateQuery<this>
  • Returns the changes that happened to the document in the format that will be sent to MongoDB.

    Returns UpdateQuery<this>

increment

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

    Returns this

init

  • init(obj: any, opts?: any, cb?: undefined | ((err: CallbackError, doc: this) => void)): this
  • 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

    • obj: any
    • Optional opts: any
    • Optional cb: undefined | ((err: CallbackError, doc: this) => void)

    Returns this

invalidate

  • invalidate(path: string, errorMsg: string | NativeError, value?: any, kind?: undefined | string): NativeError | null
  • Marks a path as invalid, causing validation to fail.

    Parameters

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

    Returns NativeError | null

isDirectModified

  • isDirectModified(path: string): boolean
  • Returns true if path was directly set and modified, else false.

    Parameters

    • path: string

    Returns boolean

isDirectSelected

  • isDirectSelected(path: string): boolean
  • Checks if path was explicitly selected. If no projection, always returns true.

    Parameters

    • path: string

    Returns boolean

isInit

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

    Parameters

    • path: string

    Returns boolean

isModified

  • isModified(path?: string | Array<string>): boolean
  • Returns true if any of the given paths is modified, else false. If no arguments, returns true if any path in this document is modified.

    Parameters

    • Optional path: string | Array<string>

    Returns boolean

isSelected

  • isSelected(path: string): boolean
  • Checks if path was selected in the source query which initialized this document.

    Parameters

    • path: string

    Returns boolean

markModified

  • markModified(path: string, scope?: any): void
  • Marks the path as having pending changes to write to the db.

    Parameters

    • path: string
    • Optional scope: any

    Returns void

model

  • model<T>(name: string): T
  • Returns another Model instance.

    Type parameters

    • T: Model<any>

    Parameters

    • name: string

    Returns T

modifiedPaths

  • modifiedPaths(options?: undefined | { includeChildren?: undefined | false | true }): Array<string>
  • Returns the list of paths that have been modified.

    Parameters

    • Optional options: undefined | { includeChildren?: undefined | false | true }

    Returns Array<string>

overwrite

  • overwrite(obj: DocumentDefinition<this>): this
  • 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

    • obj: DocumentDefinition<this>

    Returns this

parent

  • parent(): Document | undefined
  • If this document is a subdocument or populated document, returns the document's parent. Returns undefined otherwise.

    Returns Document | undefined

populate

  • populate(path: string, callback?: undefined | ((err: CallbackError, res: this) => void)): this
  • populate(opts: PopulateOptions | Array<PopulateOptions>, callback?: undefined | ((err: CallbackError, res: this) => void)): this
  • Populates document references, executing the callback when complete. If you want to use promises instead, use this function with execPopulate().

    Parameters

    • path: string
    • Optional callback: undefined | ((err: CallbackError, res: this) => void)

    Returns this

  • Parameters

    • opts: PopulateOptions | Array<PopulateOptions>
    • Optional callback: undefined | ((err: CallbackError, res: this) => void)

    Returns this

populated

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

    Parameters

    • path: string

    Returns any

remove

  • remove(options?: QueryOptions, cb?: undefined | ((err: CallbackError, res: any) => void)): void
  • remove(options?: QueryOptions): Query<any, this>
  • Removes this document from the db.

    Parameters

    • Optional options: QueryOptions
    • Optional cb: undefined | ((err: CallbackError, res: any) => void)

    Returns void

  • Parameters

    • Optional options: QueryOptions

    Returns Query<any, this>

replaceOne

  • replaceOne(replacement?: DocumentDefinition<this>, options?: QueryOptions | null, callback?: undefined | ((err: any, res: any) => void)): Query<any, this>
  • Sends a replaceOne command with this document _id as the query selector.

    Parameters

    • Optional replacement: DocumentDefinition<this>
    • Optional options: QueryOptions | null
    • Optional callback: undefined | ((err: any, res: any) => void)

    Returns Query<any, this>

save

  • save(options?: SaveOptions): Promise<this>
  • save(options?: SaveOptions, fn?: undefined | ((err: CallbackError, doc: this) => void)): void
  • save(fn?: undefined | ((err: CallbackError, doc: this) => void)): void
  • 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

    • Optional options: SaveOptions

    Returns Promise<this>

  • Parameters

    • Optional options: SaveOptions
    • Optional fn: undefined | ((err: CallbackError, doc: this) => void)

    Returns void

  • Parameters

    • Optional fn: undefined | ((err: CallbackError, doc: this) => void)

    Returns void

set

  • set(path: string, val: any, options?: any): this
  • set(path: string, val: any, type: any, options?: any): this
  • set(value: any): this
  • Sets the value of a path, or many paths.

    Parameters

    • path: string
    • val: any
    • Optional options: any

    Returns this

  • Parameters

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

    Returns this

  • Parameters

    • value: any

    Returns this

toJSON

  • toJSON(options?: ToObjectOptions): LeanDocument<this>
  • The return value of this method is used in calls to JSON.stringify(doc).

    Parameters

    • Optional options: ToObjectOptions

    Returns LeanDocument<this>

toObject

  • toObject(options?: ToObjectOptions): LeanDocument<this>
  • Converts this document into a plain-old JavaScript object (POJO).

    Parameters

    • Optional options: ToObjectOptions

    Returns LeanDocument<this>

unmarkModified

  • unmarkModified(path: string): void
  • Clears the modified state on the specified path.

    Parameters

    • path: string

    Returns void

update

  • update(update?: UpdateQuery<this>, options?: QueryOptions | null, callback?: undefined | ((err: CallbackError, res: any) => void)): Query<any, this>
  • Sends an update command with this document _id as the query selector.

    Parameters

    • Optional update: UpdateQuery<this>
    • Optional options: QueryOptions | null
    • Optional callback: undefined | ((err: CallbackError, res: any) => void)

    Returns Query<any, this>

updateOne

  • updateOne(update?: UpdateQuery<this>, options?: QueryOptions | null, callback?: undefined | ((err: CallbackError, res: any) => void)): Query<any, this>
  • Sends an updateOne command with this document _id as the query selector.

    Parameters

    • Optional update: UpdateQuery<this>
    • Optional options: QueryOptions | null
    • Optional callback: undefined | ((err: CallbackError, res: any) => void)

    Returns Query<any, this>

validate

  • validate(pathsToValidate?: Array<string>, options?: any): Promise<void>
  • validate(callback: (err: CallbackError) => void): void
  • validate(pathsToValidate: Array<string>, callback: (err: CallbackError) => void): void
  • validate(pathsToValidate: Array<string>, options: any, callback: (err: CallbackError) => void): void
  • Executes registered validation rules for this document.

    Parameters

    • Optional pathsToValidate: Array<string>
    • Optional options: any

    Returns Promise<void>

  • Parameters

    • callback: (err: CallbackError) => void
        • (err: CallbackError): void
        • Parameters

          • err: CallbackError

          Returns void

    Returns void

  • Parameters

    • pathsToValidate: Array<string>
    • callback: (err: CallbackError) => void
        • (err: CallbackError): void
        • Parameters

          • err: CallbackError

          Returns void

    Returns void

  • Parameters

    • pathsToValidate: Array<string>
    • options: any
    • callback: (err: CallbackError) => void
        • (err: CallbackError): void
        • Parameters

          • err: CallbackError

          Returns void

    Returns void

validateSync

  • validateSync(pathsToValidate?: Array<string>, options?: any): NativeError | null
  • Executes registered validation rules (skipping asynchronous validators) for this document.

    Parameters

    • Optional pathsToValidate: Array<string>
    • Optional options: any

    Returns NativeError | null

Generated using TypeDoc