summaryrefslogtreecommitdiff
path: root/node_modules/mongoose/docs/model-definition.md
blob: 69700ffea606a3461304e6d97d006b61da1efa54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
Defining a model
================

Models are defined by passing a `Schema` instance to `mongoose.model`.

    mongoose.model('MyModel', mySchema);
    // mySchema is <a Schema>

You can easily access the `Schema` constructor from the `mongoose` singleton:

    var mongoose = require('mongoose')
      , Schema = mongoose.Schema;

    var mySchema = new Schema({
        // my props
    });

Models are then accessed from `mongoose` if you want to use a single
connection:

    // connect the `mongoose` instance
    mongoose.connect('mongodb://host/db');

    var BlogPost = mongoose.model('BlogPost');

Or from a `Connection` instance if you want to use multiple
databases/connections:

    var db = mongoose.createConnection('mongodb://host/db')
      , BlogPost = db.model('BlogPost');

**Important**: the actual interaction with the data happens with the `Model`
that you obtain through `mongoose.model` or `db.model`. That's the object that
you can instantiate or that you can call `.find()`, `.findOne()`, etc upon.
Don't confuse schemas and actual models!

## Defining your keys

The `Schema` constructor receives an object representation of your schemas as
its first parameter. If you want to add more keys later, `Schema#add` provides
the same functionality.

Your schema is constructed by passing all the
JavaScript natives that you know (String, Number, Date, Buffer) as well
as others exclusive to MongoDb (for example `Schema.ObjectId`). For details on all
SchemaTypes see the [Schema Type chapter](/docs/schematypes.html).

    var ObjectId = Schema.ObjectId;

    var PostSchema = new Schema({
        owner   : ObjectId
      , title   : String
      , date    : Date
    });

### Defining documents within documents

To define an array of documents that follows a certain schema, make the value
an array with the schema constructor inside.

For example, let's assume we want to have a collection of comments within a
blogpost, and we want them to be subject to casting, validation, and other
functionality provided by models:

    var Comment = new Schema({
        body  : String
      , date  : Date
    });

    var Post = new Schema({
        title     : String
      , comments  : [Comment]
    });

This will allow you to interact very easily with subdocuments later on. For
more information, refer to the chapter on
[embedded documents](/docs/embedded-documents.html).

### Defining custom options for keys

Each key that you define is internally mapped to a `SchemaType`. Bear in mind, a
Schema is not something that you interact directly with, but it's a way to
describe to Mongoose what your want your data to look like, and how you want
it to behave.

`SchemaType`s take care of validation, casting, defaults, and other general
options. Some functionality is exclusive to certain types of `SchemaType`s, for
example only numbers support `min` and `max` values.

In order to customize some of these options directly from the definition of
your model, set your key to an object with the format `{ type: Type, ... }`.

      var Person = new Schema({
          title   : { type: String, required: true }
        , age     : { type: Number, min: 5, max: 20 }
        , meta    : {
              likes : [String]
            , birth : { type: Date, default: Date.now }
          }
      });

Those options are functions that are called on each SchemaType.
If you want to define options later on, you could access a certain key through
the `path` function:

    Person.path('age').max(400);

    Person.path('meta.birth').set(function (v) {
      // this is a setter
    });

    Person.path('title').validate(function (v) {
      return v.length > 50;
    });

Some of the options are versatile. `default` takes a `Function` or a value.
`validate` takes a `Function` or a `RegExp`. More information on these can be
found in the [Schema Type chapter](/docs/schematypes.html).

## Beyond keys: Middleware

Middleware are special user-defined functions that are called transparently
when certain native methods are called on `Document` instances (`init`, `save`
and `remove`).

Let's say that you want to email a certain user when his document changes.
You'd then define a hook on the User schema like this:

    User.pre('save', function (next) {
      email(this.email, 'Your record has changed');
      next();
    });

More information about the specifics of middleware can be found [here](/docs/middleware.html).

## Instance Methods and Static methods

For details about defining your own custom static and instance methods read [this](/docs/methods-statics.html).

## Plugins

Schemas also support plugins. Read more about it on the [Plugins](/docs/plugins.html) page.