FIND Examples
Introducing the world
collection of countries
These examples introduce NoSQL using MonogDB and PyMongo. We will be using the find() command on the collection world:
The following is included in the examples but hidden
from pymongo import MongoClient # Import the pymongo package client = MongoClient() # Use the default client value('mongodb://localhost:27017/') client.progzoo.authenticate('scott','tiger') # Log in using the username scott and the password tiger db = client['progzoo'] # Use the 'progzoo' database
By default, find() returns the entire contents of a collection. This is equivalent to find({})
Show all the documents in world
from pymongo import MongoClient client = MongoClient() client.progzoo.authenticate('scott','tiger') db = client['progzoo'] print list(db.world.find())
from pymongo import MongoClient client = MongoClient() client.progzoo.authenticate('scott','tiger') db = client['progzoo']
print list(db.world.find({}))
It is also possible to just return the first document with find_one(). The Mongo shell the equivalent to this is findOne()
To make things easier to understand the first document of world has been made a list of keys used in these examples.
list() is a python function and should be used with find(), as find() returns a cursor object
Show the first document of world
from pymongo import MongoClient client = MongoClient() client.progzoo.authenticate('scott','tiger') db = client['progzoo'] print db.world.find_one()
from pymongo import MongoClient client = MongoClient() client.progzoo.authenticate('scott','tiger') db = client['progzoo']
print db.world.find_one()
It is also possible to skip documents and limit the amount you return.
Get the 50th document of world
from pymongo import MongoClient client = MongoClient() client.progzoo.authenticate('scott','tiger') db = client['progzoo'] print list(db.world.find().skip(49).limit(1))
from pymongo import MongoClient client = MongoClient() client.progzoo.authenticate('scott','tiger') db = client['progzoo']
print list(db.world.find().skip(49).limit(1))
Querying
By passing arguments to find() we can search for specific documents
Get all the data concerning france
from pymongo import MongoClient client = MongoClient() client.progzoo.authenticate('scott','tiger') db = client['progzoo'] print list(db.world.find({"name":"France"}))
from pymongo import MongoClient client = MongoClient() client.progzoo.authenticate('scott','tiger') db = client['progzoo']
print list(db.world.find({"name":"France"}))
By passing a second parameter to find() the output can be limited to certain field(s)
In this example 1 indicates "true" and 0 indicates "false"
A feature of MongoDB is the ObjectID or "_id".
This is a unique ID MongoDB adds to each document. Unlike other keys, it has to be explicitly set to false to be excluded from the results set.
Get the population of Germany
from pymongo import MongoClient client = MongoClient() client.progzoo.authenticate('scott','tiger') db = client['progzoo'] print list(db.world.find({"name":"Germany"},{"population":1,"_id":0}))
from pymongo import MongoClient client = MongoClient() client.progzoo.authenticate('scott','tiger') db = client['progzoo']
print list(db.world.find({"name":"Germany"},{"population":1,"_id":0}))
MongoDB also allows comparisons. Syntax:
Mongo | MySQL -------------- $eq | == $gt | > $gte | >= $lt | < $lte | <= $ne | !=, <> $in | IN $nin | NOT IN
List the countries with a population that's less than 1 million.
from pymongo import MongoClient client = MongoClient() client.progzoo.authenticate('scott','tiger') db = client['progzoo'] print list(db.world.find({"population":{"$lt":1000000}},{"name":1,"_id":0}))
from pymongo import MongoClient client = MongoClient() client.progzoo.authenticate('scott','tiger') db = client['progzoo']
print list(db.world.find({"population":{"$lt":1000000}},{"name":1,"_id":0}))
It's also possible to have multiple conditions for an $and, $or, etc. This can be done in several ways, for example:
db.<collection>.find({<first condition>,<second condition>} db.world.find({"population":{"$lt":1000000},"area":{"$gt":200000}) db.<collection>.find({"$and":[<first condition>,<second condition>]} db.world.find({"$and":[{"population":{"$lt":1000000}},{"area":{"$gt":200000}}]}
Find the country with less than 1 million people, but over 200000km2 area
from pymongo import MongoClient client = MongoClient() client.progzoo.authenticate('scott','tiger') db = client['progzoo'] print list(db.world.find({"population":{"$lt":1000000},"area":{"$gt":200000}},{"name":1,"_id":0}))
from pymongo import MongoClient client = MongoClient() client.progzoo.authenticate('scott','tiger') db = client['progzoo']
print list(db.world.find({"population":{"$lt":1000000},"area":{"$gt":200000}},{"name":1,"_id":0}))