To define a schema for model, you need to create a new instance of a Schema
object. The Schema
object definition
accepts an object that describes the schema as the first parameter and an options
object as the second parameter:
new Schema(definition, options)
The options
object defines the interaction with the collection on the MongoDB server. Table 16.1 lists the options that are most commonly used.
For example, to create a schema for a collection called students
, with a name
field that is a String
type, an average
field that is a Number
type, and a scores
field that is an Array
of Number
types, you use:
var schema = new Schema({
name: String,
average: Number,
scores: [Number]
}, {collection:'students'});