Cookies help us deliver our services. By using our services, you agree to our use of cookies. More information

FIND Examples

From NoSQLZoo
Revision as of 13:09, 14 July 2015 by 40166222 (talk | contribs)
Jump to: navigation, search

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:

A note about the setup

  from pymongo import MongoClient              # Import the pymongo package
  client = MongoClient()                       # Use thee 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.

Show the first document of world

from pymongo import MongoClient
client = MongoClient()
client.progzoo.authenticate('scott','tiger')
db = client['progzoo']

print list(db.world.find_one())

from pymongo import MongoClient client = MongoClient() client.progzoo.authenticate('scott','tiger') db = client['progzoo']

print list(db.world.find_one())

Let's return the second document by skipping the first one and limiting our find to just one result

Show the second document of world

from pymongo import MongoClient
client = MongoClient()
client.progzoo.authenticate('scott','tiger')
db = client['progzoo']

print list(db.world.find().skip(1).limit(1))

from pymongo import MongoClient client = MongoClient() client.progzoo.authenticate('scott','tiger') db = client['progzoo']

print list(db.world.find().skip(1).limit(1))

What if we want the last document in a collection? For this we can use count()
Conveniently, db.collection.find().count() and db.collection.count() are identical.

Show the amount of documents in the collection and the last document

find() returns a cursor object. A way to see the contents is to wrap a list. You can avoid this by using Mongo shell

from pymongo import MongoClient
client = MongoClient()
client.progzoo.authenticate('scott','tiger')
db = client['progzoo']

print db.world.count()
print list(db.world.find().skip(db.world.count()-1))

from pymongo import MongoClient client = MongoClient() client.progzoo.authenticate('scott','tiger') db = client['progzoo']

print db.world.count() print list(db.world.find().skip(db.world.count()-1))

Querying

By passing arguments to find() we can search for specific documents

Get all the data concerning france

SELECT *
FROM world
WHERE name = '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.

Get the population of Germany

SELECT population
FROM world
WHERE name = 'Germany'
from pymongo import MongoClient
client = MongoClient()
client.progzoo.authenticate('scott','tiger')
db = client['progzoo']

print list(db.world.find({"name":"France"},{"population":1,"_id":0}))

from pymongo import MongoClient client = MongoClient() client.progzoo.authenticate('scott','tiger') db = client['progzoo']

print list(db.world.find({"name":"France"},{"population":1,"_id":0}))