- Properties
- Methods
constructor(data)async aggregate(pipeline, [options])collection()async connect(connection, [options], [name])async count(query, [options])async createIndexes(indexSpecs, [options])async deleteMany(filter, [options])async deleteOne(filter, [options])disconnect([name])async distinct(key, query, [options])fieldsAdapter(fields)async find(query, [options])async findById(id, [options])async findByIdAndDelete(id)async findByIdAndUpdate(id, update, [options])async findOne(query, [options])async findOneAndDelete(filter, [options])async findOneAndReplace(filter, replacement, [options])async findOneAndUpdate(filter, update, [options])async insertMany(docs, [options])async insertOne(doc, [options])async pagedFind(filter, page, limit, [options])async replaceOne(filter, doc, [options])sortAdapter(sorts)async updateMany(filter, update, [options])async updateOne(filter, update, [options])validate()validate(input)with(name)
The type used to cast _id properties. Defaults to
MongoDB.ObjectID.
If you wanted to use plain strings for your document _id properties you could:
Customer._idClass = String;When you define a custom _idClass property for your model you need to pass an
_id parameter of that type when you create new documents.
const document = new Customer({
_id: 'stephen-colbert',
name: 'Stephen Colbert'
});
const customers = await Customer.insertOne(data);The name of the collection in MongoDB.
Customer.collectionName = 'customers';An array of index specifications for this model. See the index specification.
Customer.indexes = [
{ key: { name: 1 } },
{ key: { email: -1 } }
];Note: These are just the definitions, the async createIndexes(indexSpecs, [options]) method doesn't
automatically use this property. You could pass this property to that method
though:
const result = await Customer.createIndexes(Customer.indexes);An alias to
MongoDB.ObjectID.
A joi object schema. See: https://github.com/hapijs/joi
Customer.schema = Joi.object({
_id: Joi.string(),
name: Joi.string().required(),
email: Joi.string().required()
});Constructs a new instance of your class using the data provided where:
data- an object containing the fields and values of the model instance. Internallydatais passed to thevalidatefunction, throwing an error if present or assigning the value (the validated value with any type conversions and other modifiers applied) to the object as properties.
Execute an aggregation framework pipeline against the collection.
pipeline- an array containing all the aggregation framework commands.options- an optional object passed to MongoDB's nativeCollection.aggregatemethod.
Returns the underlying
MongoDB.Collection.
Connects to a MongoDB server and returns the
MongoDB.Db
where:
connection- an object where:uri- the uri of the database. See uri string docs.db- the name of the database.
options- an optional object passed toMongoClient.connect.name- an optional string to identify the connection. Used to support multiple connections along with thewith(name)method. Defaults todefault.
Returns the number of documents matching a query where:
query- a filter object used to select the documents to count.options- an optional object passed to the nativeCollection.countmethod.
Creates multiple indexes in the collection and returns the result where:
indexSpecs- an array of objects containing index specifications to be created. See the index specification.options- an optional object passed to the nativeCollection.createIndexesmethod.
Indexes are defined as a static property on your models like:
Customer.indexes = [
{ key: { name: 1 } },
{ key: { email: -1 } }
];Deletes multiple documents and returns the
Collection.deleteWriteOpResult
where:
filter- a filter object used to select the documents to delete.options- an optional object passed to MongoDB's nativeCollection.deleteManymethod.
Deletes a document and returns the
Collection.deleteWriteOpResult
where:
filter- a filter object used to select the document to delete.options- an optional object passed to MongoDB's nativeCollection.deleteOnemethod.
Closes a specific connection by name or all connections if no name is specified.
Returns a list of distinct values for the given key across a collection where:
key- a string representing the field for which to return distinct values.query- an optional query object used to limit the documents distinct applies to.
A helper method to create a fields object suitable to use with MongoDB queries where:
fields- a string with space separated field names. Fields may be prefixed with-to indicate exclusion instead of inclusion.
Returns a MongoDB friendly fields object.
Customer.fieldsAdapter('name -email');
// { name: true, email: false }Finds documents and returns an array of model instances where:
query- a query object used to select the documents.options- an optional object passed to MongoDB's nativeCollection.findmethod.
Finds a document by _id and returns a model instance where:
id- a string value of the_idto find. Theidwill be casted to the type of_idClass.options- an optional object passed to MongoDB's nativeCollection.findOnemethod.
Finds a document by _id, deletes it and returns a model instance where:
id- a string value of the_idto find. Theidwill be casted to the type of_idClass.
Finds a document by _id, updates it and returns a model instance where:
id- a string value of the_idto find. Theidwill be casted to the type of_idClass.update- an object containing the fields/values to be updated.options- an optional object passed to MongoDB's nativeCollection.findOneAndUpdatemethod. Defaults to{ returnOriginal: false }.
Finds one document matching the query and returns a model intance where:
query- a filter object used to select the document.options- an optional object passed to MongoDB's nativeCollection.findOnemethod.
Finds one document matching a filter, deletes it and returns a model instance
where:
filter- a filter object used to select the document to delete.options- an optional object passed to MongoDB's nativeCollection.findOneAndDeletemethod.
Finds one document matching a filter, deletes it and returns a model instance
where:
filter- a filter object used to select the document to delete.replacement- the document replacing the matching document.options- an optional object passed to MongoDB's nativeCollection.findOneAndReplacemethod. Defaults to{ returnOriginal: false }.
Finds one document matching a filter, updates it and returns a model instance
where:
filter- a filter object used to select the document to update.update- an object containing the fields/values to be updated.options- an optional object passed to MongoDB's nativeCollection.findOneAndUpdatemethod. Defaults to{ returnOriginal: false }.
Inserts multiple documents and returns and array of model instances where:
docs- an array of document objects to insert.options- an optional object passed to MongoDB's nativeCollection.insertManymethod.
Inserts a document and returns a model instance where:
doc- a document object to insert.options- an optional object passed to MongoDB's nativeCollection.insertOnemethod.
Finds documents and returns the results where:
filter- a filter object used to select the documents.page- a number indicating the current page.limit- a number indicating how many results should be returned.options- an optional object passed to MongoDB's nativeCollection.findmethod.
The returned value is an object where:
data- an array of model instances.pages- an object where:current- a number indicating the current page.prev- a number indicating the previous page.hasPrev- a boolean indicating if there is a previous page.next- a number indicating the next page.hasNext- a boolean indicating if there is a next page.total- a number indicating the total number of pages.
items- an object where:limit- a number indicating the how many results should be returned.begin- a number indicating what item number the results begin with.end- a number indicating what item number the results end with.total- a number indicating the total number of matching results.
Replaces a document and returns a
Collection.updateWriteOpResult
where:
filter- a filter object used to select the document to replace.doc- the document that replaces the matching document.options- an optional object passed to MongoDB's nativeCollection.replaceOnemethod.
A helper method to create a sort object suitable to use with MongoDB queries where:
sorts- a string with space separated field names. Fields may be prefixed with-to indicate decending sort order.
Returns a MongoDB friendly sort object.
Customer.sortAdapter('name -email');
// { name: 1, email: -1 }Updates multiple documents and returns a
Collection.updateWriteOpResult
where:
filter- a filter object used to select the documents to update.update- the update operations object.options- an optional object passed to MongoDB's nativeCollection.updateManymethod.
Updates a document and returns a
Collection.updateWriteOpResult
where:
filter- a filter object used to select the document to update.update- the update operations object.options- an optional object passed to MongoDB's nativeCollection.updateOnemethod.
Uses joi validation internally with the static schema property of the model
to validate the instance data and returns the validated value where:
const stephen = new Customer({
name: 'Stephen Colbert'
});
const result = stephen.validate();See: https://github.com/hapijs/joi/blob/master/API.md#validatevalue-schema-options-callback
Uses joi validation internally with the static schema property of the model
to validate the input data and returns the validated value where:
input- is the object to validate.
const data = {
name: 'Stephen Colbert'
};
const result = Customer.validate(data);See: https://github.com/hapijs/joi/blob/master/API.md#validatevalue-schema-options-callback
For use with multiple named connections (See: connect(connection, [options], [name])).
Returns an object containing a subset of the model's methods bound to the named connection.
Available methods include:
aggregatecollectioncountcreateIndexesdeleteManydeleteOnedistinctfindfindByIdfindByIdAndDeletefindByIdAndUpdatefindOnefindOneAndDeletefindOneAndReplacefindOneAndUpdateinsertManyinsertOnepagedFindreplaceOneupdateManyupdateOne
await MongoModels.connect(config.connection, config.options, 'alt');
// ...
const count = await Customer.with('alt').count({});