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.
Go to Latest
class mongoose.Schema
import { mongoose } from "https://deno.land/x/mongoose@8.6.3/types/index.d.ts";
const { Schema } = mongoose;

Constructors

new
Schema(definition?: SchemaDefinition<SchemaDefinitionType<RawDocType>, RawDocType> | DocType, options?: SchemaOptions<FlatRecord<DocType>, TInstanceMethods, TQueryHelpers, TStaticMethods, TVirtuals, THydratedDocumentType> | ResolveSchemaOptions<TSchemaOptions>)

Create a new schema

Type Parameters

optional
RawDocType = any
optional
TModelType = Model<RawDocType, any, any, any>
optional
TInstanceMethods = { }
optional
TQueryHelpers = { }
optional
TVirtuals = { }
optional
TStaticMethods = { }
optional
TSchemaOptions = DefaultSchemaOptions
optional
DocType extends ApplySchemaOptions<ObtainDocumentType<DocType, RawDocType, ResolveSchemaOptions<TSchemaOptions>>, ResolveSchemaOptions<TSchemaOptions>> = ApplySchemaOptions<ObtainDocumentType<any, RawDocType, ResolveSchemaOptions<TSchemaOptions>>, ResolveSchemaOptions<TSchemaOptions>>
optional
THydratedDocumentType = HydratedDocument<FlatRecord<DocType>, TVirtuals & TInstanceMethods>

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: AddThisParameter<TInstanceMethods, THydratedDocumentType> & 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: TModelType, ...args: any[]) => unknown; }

Object of currently defined statics on this schema.

virtuals: TVirtuals

Object of currently defined virtuals on this schema

Methods

add(obj: SchemaDefinition<SchemaDefinitionType<RawDocType>> | Schema, prefix?: string): this

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 | number, 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, IndexOptions]>

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 = THydratedDocumentType>(
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
omit<T = this>(paths: string[], options?: SchemaOptions): T

Returns a new schema that has the paths from the original schema, minus the omitted ones.

path<ResultType extends SchemaType = SchemaType<any, THydratedDocumentType>>(path: string): ResultType

Gets/sets schema paths.

path<pathGeneric extends keyof RawDocType>(path: pathGeneric): SchemaType<RawDocType[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, TModelType, 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 = THydratedDocumentType>(
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 = TModelType>(
method: "insertMany" | RegExp,
options: SchemaPostOptions & { errorHandler: true; },
fn: ErrorHandlingMiddlewareWithOption<T>,
): this
post<T = never>(
method: MongooseQueryOrDocumentMiddleware | MongooseQueryOrDocumentMiddleware[] | RegExp,
options: SchemaPostOptions & { document: false; query: false; },
fn: PostMiddlewareFunction<never, never>,
): this
post<T = never>(
method: MongooseDistinctQueryMiddleware | MongooseDistinctQueryMiddleware[],
options: SchemaPostOptions & { document: boolean; query: false; },
fn: PostMiddlewareFunction<T, QueryResultType<T>>,
): this
post<T = never>(
method: MongooseDistinctDocumentMiddleware | MongooseDistinctDocumentMiddleware[] | RegExp,
options: SchemaPostOptions & { document: false; query: true; },
fn: PostMiddlewareFunction<T, T>,
): this
post<T = THydratedDocumentType>(method: MongooseDistinctDocumentMiddleware | MongooseDistinctDocumentMiddleware[], fn: PostMiddlewareFunction<T, T>): this
post<T = THydratedDocumentType>(
method: MongooseDistinctDocumentMiddleware | MongooseDistinctDocumentMiddleware[],
options: SchemaPostOptions & SchemaPostOptions,
fn: PostMiddlewareFunction<T, T>,
): this
post<T = THydratedDocumentType>(
method: MongooseQueryOrDocumentMiddleware | MongooseQueryOrDocumentMiddleware[] | RegExp,
options: SchemaPostOptions & { document: true; query: false; },
fn: PostMiddlewareFunction<T, T>,
): this
post<T = Query<any, any>>(method: MongooseRawResultQueryMiddleware | MongooseRawResultQueryMiddleware[], fn: PostMiddlewareFunction<T, null | QueryResultType<T> | ModifyResult<QueryResultType<T>>>): this
post<T = Query<any, any>>(method: MongooseDefaultQueryMiddleware | MongooseDefaultQueryMiddleware[], fn: PostMiddlewareFunction<T, QueryResultType<T>>): this
post<T = Query<any, any>>(
method: MongooseDistinctQueryMiddleware | MongooseDistinctQueryMiddleware[],
options: SchemaPostOptions,
fn: PostMiddlewareFunction<T, QueryResultType<T>>,
): this
post<T = Query<any, any>>(
method: MongooseQueryOrDocumentMiddleware | MongooseQueryOrDocumentMiddleware[] | RegExp,
options: SchemaPostOptions & { document: false; query: true; },
fn: PostMiddlewareFunction<T, QueryResultType<T>>,
): this
post<T = THydratedDocumentType | Query<any, any>>(
method: MongooseQueryOrDocumentMiddleware | MongooseQueryOrDocumentMiddleware[] | RegExp,
options: SchemaPostOptions & { document: true; query: true; },
fn: PostMiddlewareFunction<T, T | QueryResultType<T>>,
): this
post<T = THydratedDocumentType | Query<any, any>>(method: MongooseQueryOrDocumentMiddleware | MongooseQueryOrDocumentMiddleware[] | RegExp, fn: PostMiddlewareFunction<T, T | QueryResultType<T>>): this
post<T = never>(
method: MongooseDistinctQueryMiddleware | MongooseDistinctQueryMiddleware[],
options: SchemaPostOptions & { document: boolean; query: false; },
fn: ErrorHandlingMiddlewareFunction<T>,
): this
post<T = never>(
method: MongooseDistinctDocumentMiddleware | MongooseDistinctDocumentMiddleware[],
options: SchemaPostOptions & { document: false; query: boolean; },
fn: ErrorHandlingMiddlewareFunction<T>,
): this
post<T = never>(
method: MongooseQueryOrDocumentMiddleware | MongooseQueryOrDocumentMiddleware[] | RegExp,
options: SchemaPostOptions & { document: false; query: false; },
fn: ErrorHandlingMiddlewareFunction<T>,
): this
post<T = THydratedDocumentType>(method: MongooseDistinctDocumentMiddleware | MongooseDistinctDocumentMiddleware[], fn: ErrorHandlingMiddlewareFunction<T>): this
post<T = THydratedDocumentType>(
method: MongooseDistinctDocumentMiddleware | MongooseDistinctDocumentMiddleware[],
options: SchemaPostOptions,
fn: ErrorHandlingMiddlewareFunction<T>,
): this
post<T = THydratedDocumentType>(
method: MongooseQueryOrDocumentMiddleware | MongooseQueryOrDocumentMiddleware[] | RegExp,
options: SchemaPostOptions & { document: true; query: false; },
fn: ErrorHandlingMiddlewareFunction<T>,
): this
post<T = Query<any, any>>(method: MongooseDefaultQueryMiddleware | MongooseDefaultQueryMiddleware[], fn: ErrorHandlingMiddlewareFunction<T>): this
post<T = Query<any, any>>(
method: MongooseDistinctQueryMiddleware | MongooseDistinctQueryMiddleware[],
options: SchemaPostOptions,
fn: ErrorHandlingMiddlewareFunction<T>,
): this
post<T = Query<any, any>>(
method: MongooseQueryOrDocumentMiddleware | MongooseQueryOrDocumentMiddleware[] | RegExp,
options: SchemaPostOptions & { document: false; query: true; },
fn: ErrorHandlingMiddlewareFunction<T>,
): this
post<T = THydratedDocumentType | Query<any, any>>(
method: MongooseQueryOrDocumentMiddleware | MongooseQueryOrDocumentMiddleware[] | RegExp,
options: SchemaPostOptions & { document: true; query: true; },
fn: ErrorHandlingMiddlewareFunction<T>,
): this
post<T = THydratedDocumentType | Query<any, any>>(method: MongooseQueryOrDocumentMiddleware | MongooseQueryOrDocumentMiddleware[] | RegExp, fn: ErrorHandlingMiddlewareFunction<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 = TModelType>(method: "insertMany" | RegExp, fn: PostMiddlewareFunction<T, T>): this
post<T = TModelType>(
method: "insertMany" | RegExp,
options: SchemaPostOptions,
fn: PostMiddlewareFunction<T, 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 = TModelType>(method:
| "bulkWrite"
| "createCollection"
| "insertMany"
| RegExp
, fn: ErrorHandlingMiddlewareFunction<T>
): this
post<T = TModelType>(
method:
| "bulkWrite"
| "createCollection"
| "insertMany"
| RegExp
,
options: SchemaPostOptions,
fn: ErrorHandlingMiddlewareFunction<T>,
): this
pre<T = never>(
method: "save",
options: SchemaPreOptions & { document: false; query: boolean; },
fn: PreSaveMiddlewareFunction<T>,
): this

Defines a pre hook for the model.

pre<T = never>(
method: MongooseQueryOrDocumentMiddleware | MongooseQueryOrDocumentMiddleware[] | RegExp,
options: SchemaPreOptions & { document: false; query: false; },
fn: PreMiddlewareFunction<T>,
): this
pre<T = never>(
method: MongooseDistinctQueryMiddleware | MongooseDistinctQueryMiddleware[],
options: SchemaPreOptions & { document: boolean; query: false; },
fn: PreMiddlewareFunction<T>,
): this
pre<T = never>(
method: MongooseDistinctDocumentMiddleware | MongooseDistinctDocumentMiddleware[] | RegExp,
options: SchemaPreOptions & { document: false; query: boolean; },
fn: PreMiddlewareFunction<T>,
): this
pre<T = THydratedDocumentType | Query<any, any>>(
method: MongooseQueryAndDocumentMiddleware | MongooseQueryAndDocumentMiddleware[] | RegExp,
options: SchemaPreOptions & { document: true; query: true; },
fn: PreMiddlewareFunction<T>,
): this
pre<T = THydratedDocumentType>(method: "save", fn: PreSaveMiddlewareFunction<T>): this
pre<T = THydratedDocumentType>(method: MongooseDistinctDocumentMiddleware | MongooseDistinctDocumentMiddleware[], fn: PreMiddlewareFunction<T>): this
pre<T = THydratedDocumentType>(
method: MongooseDistinctDocumentMiddleware | MongooseDistinctDocumentMiddleware[],
options: SchemaPreOptions,
fn: PreMiddlewareFunction<T>,
): this
pre<T = THydratedDocumentType>(
method: MongooseQueryAndDocumentMiddleware | MongooseQueryAndDocumentMiddleware[] | RegExp,
options: SchemaPreOptions & { document: true; },
fn: PreMiddlewareFunction<T>,
): this
pre<T = THydratedDocumentType>(
method: MongooseQueryOrDocumentMiddleware | MongooseQueryOrDocumentMiddleware[] | RegExp,
options: SchemaPreOptions & { document: true; query: false; },
fn: PreMiddlewareFunction<T>,
): this
pre<T = Query<any, any>>(method: MongooseDefaultQueryMiddleware | MongooseDefaultQueryMiddleware[], fn: PreMiddlewareFunction<T>): this
pre<T = Query<any, any>>(
method: MongooseDistinctQueryMiddleware | MongooseDistinctQueryMiddleware[],
options: SchemaPreOptions,
fn: PreMiddlewareFunction<T>,
): this
pre<T = Query<any, any>>(
method: MongooseQueryOrDocumentMiddleware | MongooseQueryOrDocumentMiddleware[] | RegExp,
options: SchemaPreOptions & { document: false; query: true; },
fn: PreMiddlewareFunction<T>,
): this
pre<T = THydratedDocumentType | Query<any, any>>(
method: MongooseQueryOrDocumentMiddleware | MongooseQueryOrDocumentMiddleware[] | RegExp,
options: SchemaPreOptions & { document: true; query: true; },
fn: PreMiddlewareFunction<T>,
): this
pre<T = THydratedDocumentType | Query<any, any>>(method: MongooseQueryOrDocumentMiddleware | MongooseQueryOrDocumentMiddleware[] | RegExp, fn: PreMiddlewareFunction<T>): this
pre<T extends Aggregate<any>>(method: "aggregate" | RegExp, fn: PreMiddlewareFunction<T>): this
pre<T = TModelType>(method: "insertMany" | RegExp, fn: (
this: T,
next: (err?: CallbackError) => void,
docs: any | Array<any>,
options?: InsertManyOptions & { lean?: boolean; },
) => void | Promise<void>
): this
pre<T = TModelType>(method: "bulkWrite" | RegExp, fn: (
this: T,
next: (err?: CallbackError) => void,
ops: Array<AnyBulkWriteOperation<any>>,
options?: mongodb.BulkWriteOptions & MongooseBulkWriteOptions,
) => void | Promise<void>
): this
pre<T = TModelType>(method: "createCollection" | RegExp, fn: (
this: T,
next: (err?: CallbackError) => void,
options?: mongodb.CreateCollectionOptions & Pick<SchemaOptions, "expires">,
) => 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.

searchIndex(description: SearchIndexDescription): this

Define a search index for 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: TModelType, ...args: any[]) => any; }): this
static(name: string, fn: (this: TModelType, ...args: any[]) => any): this

Creates a virtual type with the given name.

virtualpath<T = THydratedDocumentType>(name: string): VirtualType<T> | null

Returns the virtual type with the given name.

Static Properties

ObjectId: Schema.Types.ObjectId