-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongoose.js
41 lines (30 loc) · 974 Bytes
/
mongoose.js
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
var mongoose = require('mongoose');
//connect to mongodb
mongoose.connect('mongodb://localhost:27017/test');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function () {
console.log('open')
});
// define a schema
var kittySchema = mongoose.Schema({
name: String
});
// add functionality to our documents;
// methods must be added to the schema before compiling it with mongoose.model()
kittySchema.methods.speak = function () {
var greeting = this.name
? "Meow name is " + this.name
: "I don't hava a name";
console.log(greeting)
};
// compiling schema into a Model;
// in this case ,mongoose set collection name kittens.
var Kitten = mongoose.model('Kitten', kittySchema);
//construct documents by Model
var silence = new Kitten({name: 'Silence'});
silence.save(function (err, silence) {
if (err) return console.error(err);
silence.speak();
});
Kitten.find({ name: /^Silen/ }, callback);