Connection.prototype.model()
Parameters
name «String|Function» the model name or class extending Model
[schema] «Schema» a schema. necessary when defining a model
[collection] «String» name of mongodb collection (optional) if not given it will be induced from model name
[options] «Object»
[options.overwriteModels=false] «Boolean» If true, overwrite existing models with the same name to avoid
OverwriteModelError
Returns:
- «Model» The compiled model
Defines or retrieves a model.
const mongoose = require('mongoose');
const db = mongoose.createConnection(..);
db.model('Venue', new Schema(..));
const Ticket = db.model('Ticket', new Schema(..));
const Venue = db.model('Venue');
When no collection
argument is passed, Mongoose produces a collection name by passing the model name
to the utils.toCollectionName method. This method pluralizes the name. If you don’t like this behavior, either pass a collection name or set your schemas collection name option.
Example:
const schema = new Schema({ name: String }, { collection: 'actor' });
// or
schema.set('collection', 'actor');
// or
const collectionName = 'actor'
const M = conn.model('Actor', schema, collectionName)
当前内容版权归 mongoosejs 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 mongoosejs .