Schema()
Parameters
[definition] «Object|Schema|Array» Can be one of: object describing schema paths, or schema to copy, or array of objects and schemas
[options] «Object»
Inherits:
- «NodeJS EventEmitter http://nodejs.org/api/events.html#events\_class\_events\_eventemitter»
Schema constructor.
Example:
const child = new Schema({ name: String });
const schema = new Schema({ name: String, age: Number, children: [child] });
const Tree = mongoose.model('Tree', schema);
// setting schema options
new Schema({ name: String }, { _id: false, autoIndex: false })
Options:
- autoIndex: bool - defaults to null (which means use the connection’s autoIndex option)
- autoCreate: bool - defaults to null (which means use the connection’s autoCreate option)
- bufferCommands: bool - defaults to true
- bufferTimeoutMS: number - defaults to 10000 (10 seconds). If
bufferCommands
is enabled, the amount of time Mongoose will wait for connectivity to be restablished before erroring out. - capped: bool - defaults to false
- collection: string - no default
- id: bool - defaults to true
- _id: bool - defaults to true
- minimize: bool - controls document#toObject behavior when called manually - defaults to true
- read: string
- writeConcern: object - defaults to null, use to override the MongoDB server’s default write concern settings
- shardKey: object - defaults to
null
- strict: bool - defaults to true
- strictQuery: bool - defaults to false
- toJSON - object - no default
- toObject - object - no default
- typeKey - string - defaults to ‘type’
- typePojoToMixed - boolean - defaults to true. Determines whether a type set to a POJO becomes a Mixed path or a Subdocument
- useNestedStrict - boolean - defaults to false
- validateBeforeSave - bool - defaults to
true
- versionKey: string or object - defaults to “__v”
- optimisticConcurrency: bool - defaults to false. Set to true to enable optimistic concurrency.
- collation: object - defaults to null (which means use no collation)
- selectPopulatedPaths: boolean - defaults to
true
- skipVersioning: object - paths to exclude from versioning
- timestamps: object or boolean - defaults to
false
. If true, Mongoose addscreatedAt
andupdatedAt
properties to your schema and manages those properties for you. - storeSubdocValidationError: boolean - Defaults to true. If false, Mongoose will wrap validation errors in single nested document subpaths into a single validation error on the single nested subdoc’s path.
Options for Nested Schemas:
excludeIndexes
: bool - defaults tofalse
. Iftrue
, skip building indexes on this schema’s paths.
Note:
When nesting schemas, (children
in the example above), always declare the child schema first before passing it into its parent.
当前内容版权归 mongoosejs 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 mongoosejs .