Database Reference
In-Depth Information
Listing 3.3
Get all of the Tweets in a collection
> db.tweets.find()
{ "_id" : ObjectId( "51e6d70cd13954bd0dd9e09d" ), ... }
{ "_id" : ObjectId( "51e6d70cd13954bd0dd9e09e" ), ... }
...
has more
Source: Chapter3/find_all_tweets.js
Listing 3.4
Get all of the Tweets from a single hour
> var NOVEMBER = 10; //Months are zero-indexed.
> var query = {
...
"timestamp" :{
...
"$gte" : +new Date(2011, NOVEMBER, 15, 10),
...
"$lt" : +new Date(2011, NOVEMBER, 16, 11)
...
}
... };
> db.tweets.find(query).count();
22169
Source: Chapter3/tweets_from_one_hour.js
Listing 3.5
Sort Tweets by time published
> db.tweets.find().sort({ "timestamp" : -1})
{ "_id" : ObjectId( "51e6d713d13954bd0ddaa097" ), ... }
{ "_id" : ObjectId( "51e6d713d13954bd0ddaa096" ), ... }
has more
Source: Chapter3/most_recent_tweets.js
3.10
Sorting Documents: Finding the Most Recent Tweets
To find the most recent Tweets, we will have to sort the data. MongoDB provides a
sort function that will order the Tweet by a specified field. Listing 3.5 shows an
example of how to use sort to order data by timestamp. Because we used β€œ
”in
the value of the key value pair, MongoDB will return the data in descending order.
For ascending order, use β€œ1”.
Without the index created in Sect. 3.7 , we would have caused the error shown in
Listing 3.6 . Even with a relatively small collection, MongoDB cannot sort the data
in a manageable amount of time, however with an index it is very fast.
1
 
Search WWH ::




Custom Search