-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
index.pug
132 lines (102 loc) · 4.31 KB
/
index.pug
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
extends layout
block append style
style.
hr {
display: block;
margin-top: 40px;
margin-bottom: 40px;
border: 0px;
height: 1px;
background-color: #232323;
width: 100%;
}
block content
:markdown
## Getting Started
_First be sure you have [MongoDB](http://www.mongodb.org/downloads) and [Node.js](http://nodejs.org/) installed._
Next install Mongoose from the command line using `npm`:
```
$ npm install mongoose
```
Now say we like fuzzy kittens and want to record every kitten we ever meet
in MongoDB.
The first thing we need to do is include mongoose in our project and open a
connection to the `test` database on our locally running instance of MongoDB.
```javascript
// getting-started.js
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test', {useNewUrlParser: true});
```
We have a pending connection to the test database running on localhost.
We now need to get notified if we connect successfully or if a connection
error occurs:
```javascript
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
// we're connected!
});
```
Once our connection opens, our callback will be called. For brevity,
let's assume that all following code is within this callback.
With Mongoose, everything is derived from a [Schema](/docs/guide.html).
Let's get a reference to it and define our kittens.
```javascript
const kittySchema = new mongoose.Schema({
name: String
});
```
So far so good. We've got a schema with one property, `name`, which will be a `String`. The next step is compiling our schema into a [Model](/docs/models.html).
```javascript
const Kitten = mongoose.model('Kitten', kittySchema);
```
A model is a class with which we construct documents.
In this case, each document will be a kitten with properties and behaviors as declared in our schema.
Let's create a kitten document representing the little guy we just met on the sidewalk outside:
```javascript
const silence = new Kitten({ name: 'Silence' });
console.log(silence.name); // 'Silence'
```
Kittens can meow, so let's take a look at how to add "speak" functionality
to our documents:
```javascript
// NOTE: methods must be added to the schema before compiling it with mongoose.model()
kittySchema.methods.speak = function () {
const greeting = this.name
? "Meow name is " + this.name
: "I don't have a name";
console.log(greeting);
}
const Kitten = mongoose.model('Kitten', kittySchema);
```
Functions added to the `methods` property of a schema get compiled into
the `Model` prototype and exposed on each document instance:
```javascript
const fluffy = new Kitten({ name: 'fluffy' });
fluffy.speak(); // "Meow name is fluffy"
```
We have talking kittens! But we still haven't saved anything to MongoDB.
Each document can be saved to the database by calling its [save](/docs/api.html#model_Model-save) method. The first argument to the callback will be an error if any occurred.
```javascript
fluffy.save(function (err, fluffy) {
if (err) return console.error(err);
fluffy.speak();
});
```
Say time goes by and we want to display all the kittens we've seen.
We can access all of the kitten documents through our Kitten [model](/docs/models.html).
```javascript
Kitten.find(function (err, kittens) {
if (err) return console.error(err);
console.log(kittens);
})
```
We just logged all of the kittens in our db to the console.
If we want to filter our kittens by name, Mongoose supports MongoDBs rich [querying](/docs/queries.html) syntax.
```javascript
Kitten.find({ name: /^fluff/ }, callback);
```
This performs a search for all documents with a name property that begins
with "Fluff" and returns the result as an array of kittens to the callback.
### Congratulations
That's the end of our quick start. We created a schema, added a custom document method, saved and queried kittens in MongoDB using Mongoose. Head over to the [guide](guide.html), or [API docs](api.html) for more.