-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongo.js
44 lines (39 loc) · 1.05 KB
/
mongo.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
var nmnLocation = '/path/to/node-mongodb-native'
var Db = require(nmnLocation).Db,
Server = require(nmnLocation).Server;
var host = 'localhost';
var port = 27017;
var getTweetsCollection = function(callback) {
var db = new Db('minitwitter', new Server(host, port))
db.open(function() {
db.createCollection('tweets', function() {
db.collection('tweets', function(error, collection) {
console.log(error)
callback(collection)
db.close()
})
})
})
}
this.createTweet = function(tweet, callback) {
getTweetsCollection(function(collection) {
collection.insert(tweet)
})
}
this.listTweets = function(search, callback) {
var filter = {}
if(search.message) {
filter.message = search.message.replace(/[^\w\s]/g, '')
filter.message = new RegExp('.*' + filter.message + '.*')
}
getTweetsCollection(function(collection) {
collection.find(filter, {sort: [['date', 'descending']]}, function(error, docs) {
var result = []
docs.each(function(i, doc) {
if(doc == null)
callback(result)
else result.push(doc)
})
})
})
}