I save tweets to mongo DB:
twit.stream('statuses/filter', {'track': ['animal']}, function(stream) {
stream.on('data', function(data) {
console.log(util.inspect(data));
data.created_at = new Date(data.created_at);
collectionAnimal.insert(data, function(err, docs) {});
});
});
It's OK.
The tweet time in MongoDB is in format: 2014-04-25 11:45:14 GMT (column created_at) Now I need group column created_at in hours. I would like to have the result:
hour | count tweets in hour
1 | 28
2 | 26
3 | 32
4 | 42
5 | 36
...
My unsuccessful attempt:
$keys = array('created_at' => true);
$initial = array('count' => 0);
$reduce = "function(doc, prev) { prev.count += 1 }";
$tweetsGroup = $this->collectionAnimal->group( $keys, $initial, $reduce );
But my not able to group by hour.
How to do it?
Time Series Data in MongoDB You can create a new time series collection with the createCollection() command. When you want to create a time series collection, you must include the timeField option. timeField indicates the name of the field that includes the date in each document.
Date() returns the current date as a string in mongosh. new Date() returns the current date as a Date object. mongosh wraps the Date object with the ISODate helper. The ISODate is in UTC.
Pass the arguments to $multiply in an array. The $multiply expression has the following syntax: { $multiply: [ <expression1>, <expression2>, ... ] } The arguments can be any valid expression as long as they resolve to numbers.
Timestamps. BSON has a special timestamp type for internal MongoDB use and is not associated with the regular Date type. This internal timestamp type is a 64 bit value where: the most significant 32 bits are a time_t value (seconds since the Unix epoch)
I could tell you how you can group using aggregation framework directly on mongo console
db.tweets.aggregate(
{ "$project": {
"y":{"$year":"$created_at"},
"m":{"$month":"$created_at"},
"d":{"$dayOfMonth":"$created_at"},
"h":{"$hour":"$created_at"},
"tweet":1 }
},
{ "$group":{
"_id": { "year":"$y","month":"$m","day":"$d","hour":"$h"},
"total":{ "$sum": "$tweet"}
}
})
For more options you can look here: http://docs.mongodb.org/manual/reference/operator/aggregation-date/
You will also need to find appropriate way of of using aggregation framework from whichever programming language you are using.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With