This document provides an overview of querying and aggregation on MongoDB. It discusses querying concepts like find(), indexes, and $where queries. It also covers aggregation methods like count, sum, distinct, and group to perform more complex aggregation by grouping documents. Examples are given for common queries and aggregations including counting documents, finding population sums by state, and finding largest/smallest cities by state.
6. Querying
Adhoc queries using find() or findOne() functions
You can query for ranges, set inclusion, inequalities, and more by using $
conditionals.
For more complex queries you can use $where clause
All queries return a cursor
You can make operations on that specific cursor
7. Querying
Simple examples:
db.zips.find({"state": "AL"})
db.zibs.find({"state": "AL"}, {"city":1, "pop":1})
Using "$lt", "$lte", "$gt", "$gte“, "$in", "$or", "$eq", "$not":
db.zips.find({"state": "AL", "pop": {"$lt": 500}}, {"loc":0})
db.zips.find({"state": {"$in": ["AL", "NY"]}, "pop": {"$lt": 50}})
db.zips.find({"$or": [{"pop": {"$lt": 1}}, {"city": "NEW YORK"}]}, {"city":1, "state":1, "pop":1})
NULL match itself but also match a key that doesn’t exists
We can use regular expressions: db.zips.find({"city": /new .*/i})
We can query arrays and embedded documents
8. Querying
$Where queries:
Used when we are dealing with more complex queries
Are using JavaScript
Slower than regular queries because they convert every BSON object to a JavaScript object
Example
db.zips.insert({"_id":"2", "city":"Alba", "county":"Alba", "country":"Romania", "museums": ["Muzeul de
Istorie", "Muzeul Unirii"]})
db.zips.find({$where: function() { return this.city == this.county }})
Cursors
Used same as on SQL; we can limit the results, fetch the results and apply JavaScript logic on them
9. Indexes
MongoDB’s indexes work almost identically to typical relational database
indexes
Using a query optimizer to find the best index
We can create indexes on embedded documents
Unique indexes are possible
Compound unique indexes
Geospatial indexes
10. Indexes
db.collection.find().explain() – give us information about indexes
db.collection.ensureIndex({“key”:<order>}) – create a new index
db.system.indexes.find() – check all the indexes
db.collection.dropIndex (“indexname”) – drop the index
There is another type of query that is becoming increasingly common
(especially with the emergence of mobile devices): finding the nearest N
things to a current location. MongoDB provides a special type of index for
coordinate plane queries, called a geospatial index.
11. Aggregation
Count: returns the number of documents from the collection
Sum: returns the sum of all the values for a specified field in the grouped
document
Distinct: find the distinctive values for a specific key
Group allows you to perform more complex aggregation. You choose a key
to group by, and MongoDB divides the collection into separate groups for
each value of the chosen key. For each group, you can create a result
document by aggregating the documents that are members of that group.
12. Aggregation
Count
db.zips.count()
db.zips.count({"state":"NY"})
Distinct
db.runCommand({"distinct" : "zips", "key" : "state"})
Group: SQL equivlants:
WHERE $match GROUP BY $group
HAVING $match SELECT $project
ORDER BY $sort LIMIT $limit
SUM() $sum COUNT() $sum
13. Aggregation
Group examples
Population by State
db.zips.aggregate( { $group :
{ _id : "$state",
totalPop : { $sum : "$pop" } } })
Population by State with condition
db.zips.aggregate( { $group :
{ _id : "$state",
totalPop : { $sum : "$pop" } } },
{ $match : {totalPop : { $gte : 10*1000*1000 } } }