-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmongoose_test.js
More file actions
55 lines (45 loc) · 1.34 KB
/
mongoose_test.js
File metadata and controls
55 lines (45 loc) · 1.34 KB
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
// mongoose_test.js
// In mongo shell, use db.blogposts.find(); to display all documents.
// Published as https://github.com/MikeTheTechie/node-test
var sys=require('sys');
var mongoose=require('mongoose');
sys.debug("Starting ...");
var Schema = mongoose.Schema
, ObjectId = Schema.ObjectId;
var Comments = new Schema({
title : String
, body : String
, date : Date
});
var BlogPost = new Schema({
author : String
, title : String
, body : String
, date : Date
, comments : [Comments]
, meta : {
votes : Number
, favs : Number
}
});
// define the model for the collection
mongoose.model('BlogPost', BlogPost);
mongoose.connect('mongodb://localhost/my_blogs');
// Once we define a model through `mongoose.model('ModelName', mySchema)`, we can access it through the same function
var myBlogPost = mongoose.model('BlogPost');
// We can then instantiate it, and save it:
var post = new myBlogPost();
post.author='mike';
// create a comment
post.comments.push({ title: 'My comment', body: 'My body text' });
// save the document to the collection
post.save(function (err) {
if (err) {
sys.debug("Error returned by save. " + err );
}
else
{sys.debug("Worked!");};
// close the node process. I'm not 100% sure why this is necessary - todo review/investigate.
process.exit(1);
//
});