Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/mongoose/types/index.d.ts>mongoose.Schema

MongoDB object modeling designed to work in an asynchronous environment.
Latest
class mongoose.Schema
import { mongoose } from "https://deno.land/x/mongoose@6.13.4/types/index.d.ts";
const { Schema } = mongoose;

Constructors

Create a new schema

Type Parameters

optional
EnforcedDocType = any
optional
M = Model<EnforcedDocType, any, any, any>
optional
TInstanceMethods = { }
optional
TQueryHelpers = { }
optional
TVirtuals = { }
optional
TStaticMethods = { }
optional
TSchemaOptions extends ResolveSchemaOptions<TSchemaOptions> = DefaultSchemaOptions
optional
DocType extends ApplySchemaOptions<ObtainDocumentType<DocType, EnforcedDocType, TSchemaOptions>, TSchemaOptions> = ApplySchemaOptions<ObtainDocumentType<any, EnforcedDocType, TSchemaOptions>, TSchemaOptions>

Properties

childSchemas: { schema: Schema; model: any; }[]

Array of child schemas (from document arrays and single nested subdocs) and their corresponding compiled models. Each element of the array is an object with 2 properties: schema and model.

optional
discriminators: { [name: string]: Schema; }

Object containing discriminators defined on this schema

methods: [F in keyof TInstanceMethods]: TInstanceMethods[F] & AnyObject

Object of currently defined methods on this schema.

The original object passed to the schema constructor

paths: { [key: string]: SchemaType; }

Lists all paths and their type in the schema.

Object of currently defined query helpers on this schema.

statics: [F in keyof TStaticMethods]: TStaticMethods[F] & { [name: string]: (this: M, ...args: any[]) => any; }

Object of currently defined statics on this schema.

virtuals: TVirtuals

Object of currently defined virtuals on this schema

Methods

Adds key path / schema type pairs to this schema.

alias(path: string, alias: string | string[]): this

Add an alias for path. This means getting or setting the alias is equivalent to getting or setting the path.

clearIndexes(): this

Removes all indexes on this schema

clone<T = this>(): T

Returns a copy of this schema

discriminator<DisSchema = Schema>(name: string, schema: DisSchema): this
eachPath(fn: (path: string, type: SchemaType) => void): this

Iterates the schemas paths similar to Array#forEach.

get<K extends keyof SchemaOptions>(key: K): SchemaOptions[K]

Gets a schema option.

index(fields: IndexDefinition, options?: IndexOptions): this

Defines an index (most likely compound) for this schema.

indexes(): Array<IndexDefinition>

Returns a list of indexes that this schema declares, via schema.index() or by index: true in a path's options.

loadClass(model: Function, onlyVirtuals?: boolean): this

Loads an ES6 class into a schema. Maps setters + getters, static methods, and instance methods to schema virtuals, statics, and methods.

method<Context = any>(
name: string,
fn: (this: Context, ...args: any[]) => any,
opts?: any,
): this

Adds an instance method to documents constructed from Models compiled from this schema.

method(obj: Partial<TInstanceMethods>): this
path<ResultType extends SchemaType = SchemaType<any, HydratedDocument<DocType, TInstanceMethods>>>(path: string): ResultType

Gets/sets schema paths.

path<pathGeneric extends keyof EnforcedDocType>(path: pathGeneric): SchemaType<EnforcedDocType[pathGeneric]>
path(path: string, constructor: any): this
pathType(path: string): string

Returns the pathType of path for this schema.

pick<T = this>(paths: string[], options?: SchemaOptions): T

Returns a new schema that has the picked paths from this schema.

plugin<PFunc extends PluginFunction<DocType, M, any, any, any, any>, POptions extends Parameters<PFunc>[1] = Parameters<PFunc>[1]>(fn: PFunc, opts?: POptions): this

Registers a plugin for this schema.

post<T = Query<any, any>>(
method: MongooseQueryMiddleware | MongooseQueryMiddleware[] | RegExp,
options: SchemaPostOptions & { errorHandler: true; },
fn: ErrorHandlingMiddlewareWithOption<T>,
): this

Defines a post hook for the model.

post<T = HydratedDocument<DocType, TInstanceMethods>>(
method: MongooseDocumentMiddleware | MongooseDocumentMiddleware[] | RegExp,
options: SchemaPostOptions & { errorHandler: true; },
fn: ErrorHandlingMiddlewareWithOption<T>,
): this
post<T extends Aggregate<any>>(
method: "aggregate" | RegExp,
options: SchemaPostOptions & { errorHandler: true; },
fn: ErrorHandlingMiddlewareWithOption<T, Array<any>>,
): this
post<T = M>(
method: "insertMany" | RegExp,
options: SchemaPostOptions & { errorHandler: true; },
fn: ErrorHandlingMiddlewareWithOption<T>,
): this
post<T = Query<any, any>>(method: MongooseQueryMiddleware | MongooseQueryMiddleware[] | RegExp, fn: PostMiddlewareFunction<T, QueryResultType<T>>): this
post<T = Query<any, any>>(
method: MongooseQueryMiddleware | MongooseQueryMiddleware[] | RegExp,
options: SchemaPostOptions,
fn: PostMiddlewareFunction<T, QueryResultType<T>>,
): this
post<T = HydratedDocument<DocType, TInstanceMethods>>(method: MongooseDocumentMiddleware | MongooseDocumentMiddleware[] | RegExp, fn: PostMiddlewareFunction<T, T>): this
post<T = HydratedDocument<DocType, TInstanceMethods>>(
method: MongooseDocumentMiddleware | MongooseDocumentMiddleware[] | RegExp,
options: SchemaPostOptions,
fn: PostMiddlewareFunction<T, T>,
): this
post<T extends Aggregate<any>>(method: "aggregate" | RegExp, fn: PostMiddlewareFunction<T, Array<AggregateExtract<T>>>): this
post<T extends Aggregate<any>>(
method: "aggregate" | RegExp,
options: SchemaPostOptions,
fn: PostMiddlewareFunction<T, Array<AggregateExtract<T>>>,
): this
post<T = M>(method: "insertMany" | RegExp, fn: PostMiddlewareFunction<T, T>): this
post<T = M>(
method: "insertMany" | RegExp,
options: SchemaPostOptions,
fn: PostMiddlewareFunction<T, T>,
): this
post<T = Query<any, any>>(method: MongooseQueryMiddleware | MongooseQueryMiddleware[] | RegExp, fn: ErrorHandlingMiddlewareFunction<T>): this
post<T = Query<any, any>>(
method: MongooseQueryMiddleware | MongooseQueryMiddleware[] | RegExp,
options: SchemaPostOptions,
fn: ErrorHandlingMiddlewareFunction<T>,
): this
post<T = HydratedDocument<DocType, TInstanceMethods>>(method: MongooseDocumentMiddleware | MongooseDocumentMiddleware[] | RegExp, fn: ErrorHandlingMiddlewareFunction<T>): this
post<T = HydratedDocument<DocType, TInstanceMethods>>(
method: MongooseDocumentMiddleware | MongooseDocumentMiddleware[] | RegExp,
options: SchemaPostOptions,
fn: ErrorHandlingMiddlewareFunction<T>,
): this
post<T extends Aggregate<any>>(method: "aggregate" | RegExp, fn: ErrorHandlingMiddlewareFunction<T, Array<any>>): this
post<T extends Aggregate<any>>(
method: "aggregate" | RegExp,
options: SchemaPostOptions,
fn: ErrorHandlingMiddlewareFunction<T, Array<any>>,
): this
post<T = M>(method: "insertMany" | RegExp, fn: ErrorHandlingMiddlewareFunction<T>): this
post<T = M>(
method: "insertMany" | RegExp,
options: SchemaPostOptions,
fn: ErrorHandlingMiddlewareFunction<T>,
): this
pre<T = HydratedDocument<DocType, TInstanceMethods>>(
method: DocumentOrQueryMiddleware | DocumentOrQueryMiddleware[],
options: SchemaPreOptions & { document: true; query: false; },
fn: PreMiddlewareFunction<T>,
): this

Defines a pre hook for the model.

pre<T = Query<any, any>>(
method: DocumentOrQueryMiddleware | DocumentOrQueryMiddleware[],
options: SchemaPreOptions & { document: false; query: true; },
fn: PreMiddlewareFunction<T>,
): this
pre<T = HydratedDocument<DocType, TInstanceMethods>>(method: "save", fn: PreSaveMiddlewareFunction<T>): this
pre<T = HydratedDocument<DocType, TInstanceMethods>>(
method: "save",
options: SchemaPreOptions,
fn: PreSaveMiddlewareFunction<T>,
): this
pre<T = Query<any, any>>(method: MongooseQueryMiddleware | MongooseQueryMiddleware[] | RegExp, fn: PreMiddlewareFunction<T>): this
pre<T = Query<any, any>>(
method: MongooseQueryMiddleware | MongooseQueryMiddleware[] | RegExp,
options: SchemaPreOptions,
fn: PreMiddlewareFunction<T>,
): this
pre<T = HydratedDocument<DocType, TInstanceMethods>>(method: MongooseDocumentMiddleware | MongooseDocumentMiddleware[] | RegExp, fn: PreMiddlewareFunction<T>): this
pre<T = HydratedDocument<DocType, TInstanceMethods>>(
method: MongooseDocumentMiddleware | MongooseDocumentMiddleware[] | RegExp,
options: SchemaPreOptions,
fn: PreMiddlewareFunction<T>,
): this
pre<T extends Aggregate<any>>(method: "aggregate" | RegExp, fn: PreMiddlewareFunction<T>): this
pre<T extends Aggregate<any>>(
method: "aggregate" | RegExp,
options: SchemaPreOptions,
fn: PreMiddlewareFunction<T>,
): this
pre<T = M>(method: "insertMany" | RegExp, fn: (
this: T,
next: (err?: CallbackError) => void,
docs: any | Array<any>,
) => void | Promise<void>
): this
pre<T = M>(
method: "insertMany" | RegExp,
options: SchemaPreOptions,
fn: (
this: T,
next: (err?: CallbackError) => void,
docs: any | Array<any>,
) => void | Promise<void>
,
): this
queue(name: string, args: any[]): this

Adds a method call to the queue.

remove(paths: string | Array<string>): this

Removes the given path (or [paths]).

remove(index: string | AnyObject): this

Removes index by name or index spec

requiredPaths(invalidate?: boolean): string[]

Returns an Array of path strings that are required by this schema.

set<K extends keyof SchemaOptions>(
key: K,
value: SchemaOptions[K],
_tags?: any,
): this

Sets a schema option.

static<K extends keyof TStaticMethods>(name: K, fn: TStaticMethods[K]): this

Adds static "class" methods to Models compiled from this schema.

static(obj: [F in keyof TStaticMethods]: TStaticMethods[F] & { [name: string]: (this: M, ...args: any[]) => any; }): this
static(name: string, fn: (this: M, ...args: any[]) => any): this

Creates a virtual type with the given name.

Returns the virtual type with the given name.