-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery.js
More file actions
41 lines (32 loc) · 1004 Bytes
/
Copy pathquery.js
File metadata and controls
41 lines (32 loc) · 1004 Bytes
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
/*
Connects to mongo and query data
*/
var MongoClient = require('mongodb').MongoClient;
var waterfall = require('async').waterfall;
module.exports = function(ctx, cb) {
var MONGO_URL = ctx.data.MONGO_URL;
if (!MONGO_URL) return cb(new Error('MONGO_URL secret is missing'))
waterfall([
function connect_to_db(done) {
MongoClient.connect(MONGO_URL, function(err, db) {
if(err) return done(err);
done(null, db);
});
},
function do_query(db, done) {
delete ctx.data.MONGO_URL;
var cursor = db
.collection('users')
.find( ctx.data );
var result = [];
cursor.each(function(err, doc) {
if (doc != null) {
delete doc._id;
result.push(doc);
} else {
done(null, JSON.stringify(result));
}
});
}
], cb);
};