... | ... | @@ -2,35 +2,35 @@ Basic MongoDB queries on the tweets collection: |
|
|
|
|
|
use twitter
|
|
|
|
|
|
find all (first 50)
|
|
|
Find all (first 50)
|
|
|
|
|
|
db.tweets.find()
|
|
|
|
|
|
count all
|
|
|
Count all tweets in a collection
|
|
|
|
|
|
db.tweets.find().count()
|
|
|
|
|
|
find those satysfying a condition (date, language) and count them
|
|
|
Find those satysfying a condition (date, language) and count them
|
|
|
|
|
|
db.tweets.find({"created_at" : {$gte : ISODate("2014-05-01T00:00:00:000Z") }})
|
|
|
db.tweets.find({"created_at" : {$gte : ISODate("2014-05-05T00:00:00:000Z"), $lt: ISODate("2014-05-06T00:00:00:000Z")}}).count()
|
|
|
db.tweets.find( lang : "en"}).count()
|
|
|
db.tweets.find({"created_at" : {$gte : ISODate("2014-05-05T00:00:00:000Z"), $lt: ISODate("2014-05-06T00:00:00:000Z")}, lang : "en"}).count()
|
|
|
|
|
|
(ascending) index on filed "created_at" to improve performance
|
|
|
Index on filed "created_at" to improve performance
|
|
|
|
|
|
db.twitter.ensureIndex( { created_at: 1 } )
|
|
|
|
|
|
fulltex index on the field "text"
|
|
|
Fulltex index on the field "text"
|
|
|
|
|
|
db.tweets.ensureIndex( { text: "text" } )
|
|
|
|
|
|
fulltext search
|
|
|
Fulltext search
|
|
|
|
|
|
db.tweets.runCommand( "text", {search: "usd"} )
|
|
|
db.tweets.find({ $text:{$search: "usd"}})
|
|
|
|
|
|
the aggregation framework for pipelines, count number of tweets per day
|
|
|
The aggregation framework: count number of tweets per day, sort by day
|
|
|
|
|
|
db.tweets.aggregate(
|
|
|
[
|
... | ... | @@ -49,7 +49,7 @@ the aggregation framework for pipelines, count number of tweets per day |
|
|
)
|
|
|
|
|
|
|
|
|
count the number of tweets matching a fulltext query per day
|
|
|
Count the number of tweets matching a fulltext query per day
|
|
|
|
|
|
db.tweets.aggregate(
|
|
|
[
|
... | ... | @@ -63,7 +63,10 @@ count the number of tweets matching a fulltext query per day |
|
|
Using map-reduce
|
|
|
|
|
|
db.tweets.mapReduce(
|
|
|
function(){emit(this.created_at.toDateString(), 1)},
|
|
|
function()
|
|
|
{
|
|
|
emit(this.created_at.toDateString(), 1)
|
|
|
},
|
|
|
function(key, values){return Array.sum(values)},
|
|
|
{
|
|
|
out: "counted"
|
... | ... | |