forked from mongodb/node-mongodb-native
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblog.js
109 lines (99 loc) · 5.88 KB
/
blog.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
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
GLOBAL.DEBUG = true;
sys = require("sys");
test = require("assert");
var Db = require('../lib/mongodb').Db,
Connection = require('../lib/mongodb').Connection,
Server = require('../lib/mongodb').Server,
// BSON = require('../lib/mongodb').BSONPure;
BSON = require('../lib/mongodb').BSONNative;
var host = process.env['MONGO_NODE_DRIVER_HOST'] != null ? process.env['MONGO_NODE_DRIVER_HOST'] : 'localhost';
var port = process.env['MONGO_NODE_DRIVER_PORT'] != null ? process.env['MONGO_NODE_DRIVER_PORT'] : Connection.DEFAULT_PORT;
var LINE_SIZE = 120;
sys.puts("Connecting to " + host + ":" + port);
var db = new Db('node-mongo-blog', new Server(host, port, {}), {native_parser:true});
db.open(function(err, db) {
db.dropDatabase(function(err, result) {
sys.puts("===================================================================================");
sys.puts(">> Adding Authors");
db.collection('authors', function(err, collection) {
collection.createIndex(["meta", ['_id', 1], ['name', 1], ['age', 1]], function(err, indexName) {
sys.puts("===================================================================================");
var authors = {};
// Insert authors
collection.insert([{'name':'William Shakespeare', 'email':'william@shakespeare.com', 'age':587},
{'name':'Jorge Luis Borges', 'email':'jorge@borges.com', 'age':123}], function(err, docs) {
docs.forEach(function(doc) {
sys.puts(sys.inspect(doc));
authors[doc.name] = doc;
});
});
sys.puts("===================================================================================");
sys.puts(">> Authors ordered by age ascending");
sys.puts("===================================================================================");
collection.find({}, {'sort':[['age', 1]]}, function(err, cursor) {
cursor.each(function(err, author) {
if(author != null) {
sys.puts("[" + author.name + "]:[" + author.email + "]:[" + author.age + "]");
} else {
sys.puts("===================================================================================");
sys.puts(">> Adding users");
sys.puts("===================================================================================");
db.collection('users', function(err, userCollection) {
var users = {};
userCollection.insert([{'login':'jdoe', 'name':'John Doe', 'email':'john@doe.com'},
{'login':'lsmith', 'name':'Lucy Smith', 'email':'lucy@smith.com'}], function(err, docs) {
docs.forEach(function(doc) {
sys.puts(sys.inspect(doc));
users[doc.login] = doc;
});
});
sys.puts("===================================================================================");
sys.puts(">> Users ordered by login ascending");
sys.puts("===================================================================================");
userCollection.find({}, {'sort':[['login', 1]]}, function(err, cursor) {
cursor.each(function(err, user) {
if(user != null) {
sys.puts("[" + user.login + "]:[" + user.name + "]:[" + user.email + "]");
} else {
sys.puts("===================================================================================");
sys.puts(">> Adding articles");
sys.puts("===================================================================================");
db.collection('articles', function(err, articlesCollection) {
articlesCollection.insert([
{ 'title':'Caminando por Buenos Aires',
'body':'Las callecitas de Buenos Aires tienen ese no se que...',
'author_id':authors['Jorge Luis Borges']._id},
{ 'title':'I must have seen thy face before',
'body':'Thine eyes call me in a new way',
'author_id':authors['William Shakespeare']._id,
'comments':[{'user_id':users['jdoe']._id, 'body':"great article!"}]
}
], function(err, docs) {
docs.forEach(function(doc) {
sys.puts(sys.inspect(doc));
});
})
sys.puts("===================================================================================");
sys.puts(">> Articles ordered by title ascending");
sys.puts("===================================================================================");
articlesCollection.find({}, {'sort':[['title', 1]]}, function(err, cursor) {
cursor.each(function(err, article) {
if(article != null) {
sys.puts("[" + article.title + "]:[" + article.body + "]:[" + article.author_id.toHexString() + "]");
sys.puts(">> Closing connection");
db.close();
}
});
});
});
}
});
});
});
}
});
});
});
});
});
});