-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
36 lines (28 loc) · 872 Bytes
/
index.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
var mongoose = require('mongoose');
//connect to mongo db using mongoose
mongoose.connect('mongodb://localhost/test');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
console.log("connected!");
});
//import schema object
var Schema = mongoose.Schema;
//make new schema and pass an object
var BlogPostSchema = new Schema({
author: Schema.ObjectId,
title: String,
body: String,
date: Date
});
// 1. create a model using mongoose and give the mongodb collection a name
// 2. pass the model schema as the second parameter
var BlogPost = mongoose.model('BlogPost', BlogPostSchema);
var mypost = {
title: "Game of Thrones",
body: "The most famous dwarf in all the world",
date: new Date()
};
var post = new BlogPost(mypost);
//save to BlogPost collection database
post.save();