Databases Reference
In-Depth Information
Querying MongoDB
I used a current snapshot of my web server access log to populate a sample data set. If you don't
have access to web server logs, download the fi le named sample_access_log from the code
download bundle available with this topic.
After you have some data persisted in a Mongo instance, you are ready to query and fi lter that set.
In the previous chapter you learned some essential querying mechanisms using MongoDB. Let's
revise some of those and explore a few additional concepts that relate to queries.
All my log data is stored in a collection named logdata . To list all the records in the logdata
collection, fi re up the Mongo shell (a JavaScript shell, which can be invoked with the help of the
bin/mongo command) and query like so:
> var cursor = db.logdata.find()
> while (cursor.hasNext()) printjson(cursor.next());
This prints the data set in a nice presentable format like this:
{
“_id” : ObjectId(“4cb164b75a91870732000000”),
“http_vers” : “HTTP/1.1”,
“ident” : “-”,
“http_response_code” : “200”,
“referrer” : “-”,
“url” : “/hi/tag/2009/”,
“ip” : “123.125.66.32”,
“time” : “09/Oct/2010:07:30:01 -0600”,
“http_response_size” : “13308”,
“http_method” : “GET”,
“user_agent” : “Baiduspider+(+http://www.baidu.com/search/spider.htm)”,
“http_user“ : “-“,
“request_line“ : “GET /hi/tag/2009/ HTTP/1.1“
}
{
“_id“ : ObjectId(“4cb164b75a91870732000001“),
“http_vers“ : “HTTP/1.0“,
“ident“ : “-“,
“http_response_code“ : “200“,
“referrer“ : “-“,
“url“ : “/favicon.ico“,
“ip“ : “89.132.89.62“,
“time“ : “09/Oct/2010:07:30:07 -0600“,
“http_response_size“ : “1136“,
“http_method“ : “GET“,
“user_agent“ : “Safari/6531.9 CFNetwork/454.4 Darwin/10.0.0 (i386)
(MacBook5%2C1)“,
“http_user“ : “-“,
“request_line“ : “GET /favicon.ico HTTP/1.0“
}
...
Search WWH ::




Custom Search