-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
169 lines (132 loc) · 4.01 KB
/
test.js
File metadata and controls
169 lines (132 loc) · 4.01 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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
var mongodb = require('mongodb');
var incremental = require('./index');
var Db = mongodb.Db,
Connection = mongodb.Connection,
Server = mongodb.Server;
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 db = new Db('node-mongo-examples', new Server(host, port, {}), {});
console.log('opening', host, port);
db.open(function(err, db) {
// incremental.mapReduce({ db: db, collections: 'log*' });
//
// return;
if (err) return console.error('open:', err);
db.dropCollection('test', function(err, result) {
console.log("dropped test: ", result);
});
db.dropCollection('testResults', function(err, result) {
console.log("dropped testResults: ", result);
});
db.dropCollection('log101011', function(err, result) {
console.log("dropped log101011: ", result);
});
db.dropCollection('log101111', function(err, result) {
console.log("dropped log101111: ", result);
});
db.dropCollection('log101211', function(err, result) {
console.log("dropped log101211: ", result);
});
db.dropCollection('logs', function(err, result) {
console.log("dropped logs: ", result);
});
db.dropCollection('incmapreduce', function(err, result) {
console.log("dropped incmapreduce: ", result);
});
db.collection('test', function(err, collection) {
if (err) return console.error('collection test:', err.stack);
collection.insert([
{
username: "jones",
likes: 20,
text: "Hello world!"
},
{
username: "jimmy",
likes: 13,
text: "The James"
},
{
username: "jones",
likes: 5,
text: "Second Comment"
},
{
username: "juju",
likes: 0,
text: "beans"
}
]);
var map = function map() {
emit(this.username, { count: 1, likes: this.likes });
};
var reduce = function reduce(key, values) {
var totals = { count: 0, likes: 0 };
values.forEach(function(current) {
totals.count += current.count;
totals.likes += current.likes;
});
return totals;
};
var options = {
out: {
incremental: 'testResults'
}
};
console.log('starting map reduce');
collection.mapReduce(map, reduce, options, function(err, results) {
if (err) return console.error('mapreduce:', err);
console.log('done with comments!');
});
});
db.collection('log101011', function(err, collection) {
if (err) return console.error('collection log101011:', err);
var map = function map() {
this.tags.forEach(function(tag) {
emit(tag, 1);
});
};
var reduce = function reduce(key, values) {
var sum = 0;
values.forEach(function(value) {
sum += value;
});
return sum;
};
var options = {
out: {
incremental: 'logs'
}
};
collection.insert([
{ tags: ['one', 'two', 'three'] },
{ tags: ['one', 'four', 'five'] },
{ tags: ['two', 'six', 'seven'] }
]);
db.collection('log101111', function(err, collection) {
if (err) return console.error('collection log101111:', err);
collection.insert([
{ tags: ['one', 'four', 'eight'] },
{ tags: ['three', 'four', 'five'] },
{ tags: ['one', 'two', 'four'] }
]);
incremental.mapReduce({ collections: 'log*', db: db }, map, reduce, options, function(err, results) {
if (err) return console.error('mapreduce-last:', err);
console.log('done with 2 logs!');
db.collection('log101211', function(err, collection) {
if (err) return console.error('collection log 101211:', err);
collection.insert([
{ tags: ['one', 'two', 'three'] },
{ tags: ['one', 'four', 'five'] },
{ tags: ['two', 'six', 'seven'] }
]);
incremental.mapReduce({ collections: 'log*', db: db }, map, reduce, options, function(err, results) {
if (err) return console.error('mapreduce-last:', err);
console.log('done with logs!');
db.close();
});
});
});
});
});
});