If this is a discriminator model, baseModelName
is the name of
the base model.
Collection the model uses.
Connection the model uses.
Registered discriminators for this model.
Event emitter that reports any errors that occurred. Useful for global error handling.
The name of the model
Schema the model uses.
Adds a $where
clause to this query
Check for user credentials, generate its token and return information
Email credentials
Password credentials
Parameters to given to the token creation (default: undefined)
The user instance if a user matches the credentials, false otherwise
const email = '...'
const password = '...'
const user = await User.authenticate(email, password, { ...tokenParams })
if(user) {
console.log('Connection successful !')
}
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 round
trip to MongoDB.
Check if a hashed password correspond to a plain text password
Password to compare to the hash
Hash to compare to the password
True if the password and the hash match, false otherwise
await User.checkPassword('password', '$2b$10$ZcXGphJNeWfjc6F0klucDe1SRvh6XOLbgAop/RoBg.U3eXEGnP6xm')
// -> true
await User.checkPassword('passw0rd', '$2b$10$ZcXGphJNeWfjc6F0klucDe1SRvh6XOLbgAop/RoBg.U3eXEGnP6xm')
// -> false
Creates a count
query: counts the number of documents that match filter
.
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.
Similar to ensureIndexes()
, except for it uses the createIndex
function.
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.
Adds a discriminator type.
Creates a distinct
query: returns the distinct values of the given field
that match filter
.
Check if a user already exist with a given email address
Email address to check
True if a user already exist with the given email address, false otherwise
const email = '...'
if(await User.doesExist(email)) {
console.log('A user already exists with this email address !')
}
Sends createIndex
commands to mongo for each index declared in the schema.
The createIndex
commands are sent in series.
Creates a estimatedDocumentCount
query: counts the number of documents in the collection.
Returns true if at least one document exists in the database that matches
the given filter
, and false otherwise.
Creates a find
query: gets a list of documents that match filter
.
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()
.
Creates a findByIdAndDelete
query, filtering by the given _id
.
Creates a findByIdAndRemove
query, filtering by the given _id
.
Creates a findOneAndUpdate
query, filtering by the given _id
.
Finds one document.
Creates a findOneAndDelete
query: atomically finds the given document, deletes it, and returns the document as it was before deletion.
Creates a findOneAndRemove
query: atomically finds the given document and deletes it.
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
.
Get a user by its token
JWT of the current session of the user, will be used to get the user id
The User if there is any user matching (matching ID from the payload AND the token), false otherwise
const token = '...'
const user = await User.getByToken(token)
if(user) {
console.log('Session reconnected via token !')
}
Hash a plain text password
Password to hash
The hashed password
const password = '...'
const hashed = await User.hashPassword(password)
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.
Lists the indexes currently defined in MongoDB. This may or may not be
the same as the indexes defined in your schema depending on whether you
use the autoIndex
option and if you
build indexes manually.
Executes a mapReduce command.
Populates document references.
Creates a replaceOne
query: finds the first document that matches filter
and replaces it with replacement
.
Starts a MongoDB session for benefits like causal consistency, retryable writes, and transactions.
Makes the indexes in MongoDB match the indexes defined in this model's
schema. This function will drop any indexes that are not defined in
the model's schema except the _id
index, and build any indexes that
are in your schema but not in MongoDB.
Translate any aliases fields/conditions so the final query or document object is pure
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
.
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.
Creates a Query, applies the passed conditions, and returns the Query.
Generated using TypeDoc
Base Mongoose instance the model uses.