import { type mongoose } from "https://deno.land/x/mongoose@8.4.3/types/models.d.ts";
const { Model } = mongoose;
Models are fancy constructors compiled from Schema
definitions.
An instance of a model is called a document.
Models are responsible for creating and reading documents from the underlying MongoDB database
Type Parameters
Properties
If this is a discriminator model, baseModelName
is the name of
the base model.
Event emitter that reports any errors that occurred. Useful for global error handling.
Registered discriminators for this model.
Schema the model uses.
Methods
Sends multiple insertOne
, updateOne
, updateMany
, replaceOne
,
deleteOne
, and/or deleteMany
operations to the MongoDB server in one
command. This is faster than sending multiple independent operations (e.g.
if you use create()
) because with bulkWrite()
there is only one network
round trip to the MongoDB server.
Sends multiple save()
calls in a single bulkWrite()
. This is faster than
sending multiple save()
calls because with bulkSave()
there is only one
network round trip to the MongoDB server.
Creates a countDocuments
query: counts the number of documents that match filter
.
Creates a new document or documents
Create the collection for this model. By default, if no indexes are specified, mongoose will not create the collection for the model until any documents are created. Use this method to create the collection explicitly.
Create an Atlas search index. This function only works when connected to MongoDB Atlas.
Deletes all of the documents that match conditions
from the collection.
Behaves like remove()
, but deletes all documents that match conditions
regardless of the single
option.
Deletes the first document that matches conditions
from the collection.
Behaves like remove()
, but deletes at most one document regardless of the
single
option.
Delete an existing Atlas search index by name. This function only works when connected to MongoDB Atlas.
Finds a single document by its _id field. findById(id)
is almost*
equivalent to findOne({ _id: id })
. If you want to query by a document's
_id
, use findById()
instead of findOne()
.
Finds one document.
Shortcut for creating a new Document from existing raw data, pre-saved in the DB. The document returned has no paths marked as modified initially.
This function is responsible for building indexes,
unless autoIndex
is turned off.
Mongoose calls this function automatically when a model is created using
mongoose.model()
or
connection.model()
, so you
don't need to call it.
Inserts one or more new documents as a single insertMany
call to the MongoDB server.
List all Atlas search indexes on this model's collection. This function only works when connected to MongoDB Atlas.
Populates document references.
Update an existing Atlas search index. This function only works when connected to MongoDB Atlas.
Casts and validates the given object against this model's schema, passing the given context
to custom validators.
Watches the underlying collection for changes using MongoDB change streams.
Adds a $where
clause to this query
Translate any aliases fields/conditions so the final query or document object is pure
Creates a distinct
query: returns the distinct values of the given field
that match filter
.
Creates a estimatedDocumentCount
query: counts the number of documents in the collection.
Returns a document with its _id
if at least one document exists in the database that matches
the given filter
, and null
otherwise.
Creates a find
query: gets a list of documents that match filter
.
Creates a findByIdAndDelete
query, filtering by the given _id
.
Creates a findOneAndUpdate
query, filtering by the given _id
.
Creates a findOneAndDelete
query: atomically finds the given document, deletes it, and returns the document as it was before deletion.
Creates a findOneAndReplace
query: atomically finds the given document and replaces it with replacement
.
Creates a findOneAndUpdate
query: atomically find the first document that matches filter
and apply update
.
Creates a replaceOne
query: finds the first document that matches filter
and replaces it with replacement
.
Creates a updateMany
query: updates all documents that match filter
with update
.
Creates a updateOne
query: updates the first document that matches filter
with update
.
Creates a Query, applies the passed conditions, and returns the Query.