Skip to main content
This release is 2 versions behind 0.1.24 — the latest version of @oridune/mongo. Jump to latest
This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score
23%
Published
a week ago (0.1.22)
class MongoModel
extends MongoHooks<InputShape, OutputShape>

Constructors

new
MongoModel(
name: string,
schema: Schema | (() => Schema),
)

Type Parameters

Schema extends ObjectValidator<any, any, any>
InputShape extends object = inferInput<Schema>
OutputShape extends object = inferOutput<Schema>

Properties

readonly
collection

Get access to the collection on the native mongodb driver

A connection index determines which database to connect with.

readonly
database

Get access to the database on the native mongodb driver

protected
abstract
databaseInstance: Db
protected
abstract
populateConfig: { field: string; model: MongoModel<any, any, any>; options?: PopulateOptions<any> & { unwind?: boolean; }; }

Methods

count(
filter?:
ObjectId
| string
| Filter<InputDocument<InputShape>>
,
options?: AggregateOptions & { cache?: TCacheOptions; },
)

Count all documents given a specific filter/conditions.

create(
options?: InsertOneOptions & { validate?: boolean; },
): Promise<OutputDocument<OutputShape>>

Create a new document in the collection

createIndex(...indexDesc: (CreateIndexesOptions & { key: Partial<Record<string, IndexDirection>>; partialFilterExpression?: Filter<InputDocument<InputShape>>; })[])

Create a new index

createMany(
options?: BulkWriteOptions & { validate?: boolean; },
): Promise<OutputDocument<OutputShape>[]>

Create multiple documents in a single query

deleteMany(
filter?: Filter<InputDocument<InputShape>>,
options?: DeleteOptions,
)

Delete many documents

deleteManyOrFail(
filter?: Filter<InputDocument<InputShape>>,
options?: DeleteOptions,
)

Try to delete many documents. Throws an error if couldn't delete.

deleteOne(
filter?:
ObjectId
| string
| Filter<InputDocument<InputShape>>
,
options?: DeleteOptions,
)

Delete a single document

deleteOneOrFail(
filter?:
ObjectId
| string
| Filter<InputDocument<InputShape>>
,
options?: DeleteOptions,
)

Try to delete a single document. Throws an error if couldn't delete.

dropIndex(
indexNames: string[],
options?: CommandOperationOptions,
)

Drop an index

exists(
filter?:
ObjectId
| string
| Filter<InputDocument<InputShape>>
,
options?: CountDocumentsOptions & { cache?: TCacheOptions; },
)

Check if document(s) exists.

find(
filter?: Filter<InputDocument<InputShape>>,
options?: AggregateOptions & { cache?: TCacheOptions; },
)

Find documents from this collection

findAndDeleteMany(
filter?: Filter<InputDocument<InputShape>>,
options?: DeleteOptions,
)

Find many and delete them

findAndDeleteOne(
filter?:
ObjectId
| string
| Filter<InputDocument<InputShape>>
,
options?: DeleteOptions,
)

Find and delete a single document, returning the document.

findAndUpdateMany(
filter?: Filter<InputDocument<InputShape>>,
updates?: UpdateFilter<InputDocument<InputShape>> & Partial<InputDocument<InputShape>>,
options?: UpdateOptions & { validate?: boolean; },
)

Find many documents and update them, returning the old version of documents.

findAndUpdateOne(
filter?:
ObjectId
| string
| Filter<InputDocument<InputShape>>
,
updates?: UpdateFilter<InputDocument<InputShape>> & Partial<InputDocument<InputShape>>,
options?: UpdateOptions & { validate?: boolean; },
)

Find a single document and update it. Returns the old verion of the document

findOne(
filter?:
ObjectId
| string
| Filter<InputDocument<InputShape>>
,
options?: AggregateOptions & { cache?: TCacheOptions; },
)

Find a single document

findOneOrFail(
filter?:
ObjectId
| string
| Filter<InputDocument<InputShape>>
,
options?: AggregateOptions & { cache?: TCacheOptions; },
)

Try to find a single document. If not found, throws an error.

Get the validator schema of this model.

Get the validator schema of this model in update state.

protected
log(
method: string,
...args: any[],
): void
populate<
F extends string | `${string}.${string}`,
M extends MongoModel<any, any, any>,
S = OutputDocument<M extends MongoModel<any, any, infer R> ? R : never>,
>
(
field: F,
model: M,
options?: PopulateOptions<M>,
): MongoModel<Schema, InputShape, PopulatedDocument<OutputShape, F, S[]>>

Populates the array of references with documents referenced of another collection.

populateOne<
F extends string | `${string}.${string}`,
M extends MongoModel<any, any, any>,
S = OutputDocument<M extends MongoModel<any, any, infer R> ? R : never>,
>
(
field: F,
model: M,
options?: PopulateOptions<M>,
): MongoModel<Schema, InputShape, PopulatedDocument<OutputShape, F, S>>

Populate a reference with a document

replaceOne(
filter?:
ObjectId
| string
| Filter<InputDocument<InputShape>>
,
options?: ReplaceOptions,
)

Replace a single document with another document

search(
searchTerm?: string | { $search: string; $language?: string; $caseSensitive?: boolean; $diacriticSensitive?: boolean; },
options?: AggregateOptions & { cache?: TCacheOptions; },
)

Search for documents on this collection using full text index

A full text search index is required.

updateAndFindMany(
filter?: Filter<InputDocument<InputShape>>,
updates?: UpdateFilter<InputDocument<InputShape>> & Partial<InputDocument<InputShape>>,
options?: UpdateOptions & { validate?: boolean; },
)

Update many documents and then return them.

updateAndFindOne(
filter?:
ObjectId
| string
| Filter<InputDocument<InputShape>>
,
updates?: UpdateFilter<InputDocument<InputShape>> & Partial<InputDocument<InputShape>>,
options?: UpdateOptions & { validate?: boolean; },
)

Update a single document and then return the updated document

updateMany(
filter?: Filter<InputDocument<InputShape>>,
updates?: UpdateFilter<InputDocument<InputShape>> & Partial<InputDocument<InputShape>>,
options?: UpdateOptions & { validate?: boolean; },
)

Update many documents

updateManyOrFail(
filter?: Filter<InputDocument<InputShape>>,
updates?: UpdateFilter<InputDocument<InputShape>> & Partial<InputDocument<InputShape>>,
options?: UpdateOptions & { validate?: boolean; },
)

Try to update many documents. Throws an error if failed or couldn't update a single document.

updateOne(
filter?:
ObjectId
| string
| Filter<InputDocument<InputShape>>
,
updates?: UpdateFilter<InputDocument<InputShape>> & Partial<InputDocument<InputShape>>,
options?: UpdateOptions & { validate?: boolean; },
)

Update a single document

updateOneOrFail(
filter?:
ObjectId
| string
| Filter<InputDocument<InputShape>>
,
updates?: UpdateFilter<InputDocument<InputShape>> & Partial<InputDocument<InputShape>>,
options?: UpdateOptions & { validate?: boolean; },
)

Try to update a single document. Throws an error if failed to update or condition didn't met.

watch(
filter?:
ObjectId
| string
| Filter<InputDocument<InputShape>>
,
options?: ChangeStreamOptions,
)

Watch for real time updates from mongodb

Add Package

deno add jsr:@oridune/mongo

Import symbol

import { MongoModel } from "@oridune/mongo";

---- OR ----

Import directly with a jsr specifier

import { MongoModel } from "jsr:@oridune/mongo";

Add Package

npx jsr add @oridune/mongo

Import symbol

import { MongoModel } from "@oridune/mongo";

Add Package

yarn dlx jsr add @oridune/mongo

Import symbol

import { MongoModel } from "@oridune/mongo";

Add Package

pnpm dlx jsr add @oridune/mongo

Import symbol

import { MongoModel } from "@oridune/mongo";

Add Package

bunx jsr add @oridune/mongo

Import symbol

import { MongoModel } from "@oridune/mongo";