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

Difference between revisions of "FIND Examples"

From NoSQLZoo
Jump to: navigation, search
Line 20: Line 20:
 
#PRETTY
 
#PRETTY
 
import pprint
 
import pprint
out = pprint.PrettyPrinter(indent=4)
+
pp = pprint.PrettyPrinter(indent=4)
 
</pre>
 
</pre>
 
   </div>
 
   </div>
Line 31: Line 31:
 
<p class=strong>Show all the documents in world</p>
 
<p class=strong>Show all the documents in world</p>
 
<pre class=def>
 
<pre class=def>
print(list(db.world.find()))
+
pp.pprint(list(
 +
    db.world.find()
 +
))
 
</pre>
 
</pre>
 
<div class=ans>
 
<div class=ans>
print(list(db.world.find()))
+
pp.pprint(list(db.world.find()))
 
</div>
 
</div>
 
</div>
 
</div>
Line 50: Line 52:
 
<p class=strong>Show the first document of world</p>
 
<p class=strong>Show the first document of world</p>
 
<pre class=def>
 
<pre class=def>
print(db.world.find_one())
+
pp.pprint(db.world.find_one())
 
</pre>
 
</pre>
 
<div class=ans>
 
<div class=ans>
print(db.world.find_one())
+
pp.pprint(db.world.find_one())
 
</div>
 
</div>
 
</div>
 
</div>
Line 68: Line 70:
 
<p class=strong>Get the 50th document of world</p>
 
<p class=strong>Get the 50th document of world</p>
 
<pre class=def>
 
<pre class=def>
print(
+
pp.pprint(
 
     db.world.find()[50]
 
     db.world.find()[50]
 
)
 
)
 
</pre>
 
</pre>
 
<div class=ans>
 
<div class=ans>
print(db.world.find()[50])
+
pp.print(db.world.find()[50])
 
</div>
 
</div>
 
</div>
 
</div>
Line 82: Line 84:
 
<p class=strong>Get all the data concerning france</p>
 
<p class=strong>Get all the data concerning france</p>
 
<pre class=def>
 
<pre class=def>
print(list(
+
pp.pprint(list(
 
     db.world.find({"name":"France"})
 
     db.world.find({"name":"France"})
 
))
 
))
 
</pre>
 
</pre>
 
<div class=ans>
 
<div class=ans>
print(list(db.world.find({"name":"France"})))
+
pp.pprint(list(db.world.find({"name":"France"})))
 
</div>
 
</div>
 
</div>
 
</div>
Line 98: Line 100:
 
<p class=strong>Get the population of Germany</p>
 
<p class=strong>Get the population of Germany</p>
 
<pre class=def>
 
<pre class=def>
print(list(
+
pp.pprint(list(
 
     db.world.find({"name":"Germany"},{"population":1,"_id":0})
 
     db.world.find({"name":"Germany"},{"population":1,"_id":0})
 
))
 
))
 
</pre>
 
</pre>
 
<div class=ans>
 
<div class=ans>
print(list(db.world.find({"name":"Germany"},{"population":1,"_id":0})))
+
pp.pprint(list(db.world.find({"name":"Germany"},{"population":1,"_id":0})))
 
</div>
 
</div>
 
</div>
 
</div>
Line 123: Line 125:
 
<p class=strong>List the countries with a population that's less than 1 million.</p>
 
<p class=strong>List the countries with a population that's less than 1 million.</p>
 
<pre class=def>
 
<pre class=def>
print(list(
+
pp.pprint(list(
 
     db.world.find({"population":{"$lt":1000000}},{"name":1,"_id":0})
 
     db.world.find({"population":{"$lt":1000000}},{"name":1,"_id":0})
 
))
 
))
 
</pre>
 
</pre>
 
<div class=ans>
 
<div class=ans>
print(list(db.world.find({"population":{"$lt":1000000}},{"name":1,"_id":0})))
+
pp.pprint(list(db.world.find({"population":{"$lt":1000000}},{"name":1,"_id":0})))
 
</div>
 
</div>
 
</div>
 
</div>
Line 143: Line 145:
 
<p class=strong>Find the country with less than 1 million people, but over 200000km<sup>2</sup> area</p>
 
<p class=strong>Find the country with less than 1 million people, but over 200000km<sup>2</sup> area</p>
 
<pre class=def>
 
<pre class=def>
print(list(
+
pp.pprint(list(
 
     db.world.find({"population":{"$lt":1000000},"area":{"$gt":200000}},{"name":1,"_id":0})
 
     db.world.find({"population":{"$lt":1000000},"area":{"$gt":200000}},{"name":1,"_id":0})
 
))
 
))
 
</pre>
 
</pre>
 
<div class=ans>
 
<div class=ans>
print(list(db.world.find({"population":{"$lt":1000000},"area":{"$gt":200000}},{"name":1,"_id":0})))
+
pp.pprint(list(db.world.find({"population":{"$lt":1000000},"area":{"$gt":200000}},{"name":1,"_id":0})))
 
</div>
 
</div>
 
</div>
 
</div>
Line 156: Line 158:
 
<p class=strong>Find the continent of Brazil, the United Kingdom, and Ghana.</p>
 
<p class=strong>Find the continent of Brazil, the United Kingdom, and Ghana.</p>
 
<pre class=def>
 
<pre class=def>
print(list(
+
pp.pprint(list(
 
     db.world.find({"name":{"$in":["Brazil","United Kingdom","Ghana"]}},{"name":1,"_id":0})
 
     db.world.find({"name":{"$in":["Brazil","United Kingdom","Ghana"]}},{"name":1,"_id":0})
 
))
 
))
 
</pre>
 
</pre>
 
<div class=ans>
 
<div class=ans>
print(list(db.world.find({"name":{"$in":["Brazil","United Kingdom","Ghana"]}},{"name":1,"_id":0})))
+
pp.pprint(list(db.world.find({"name":{"$in":["Brazil","United Kingdom","Ghana"]}},{"name":1,"_id":0})))
 
</div>
 
</div>
 
</div>
 
</div>

Revision as of 13:29, 15 July 2015

Introducing the world collection of countries

These examples introduce NoSQL using MonogDB and PyMongo under Python3.4. We will be using the find() command on the collection world:

The following is included in the examples but hidden

#ENCODING
import io
import sys
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-16')
#MONGO
from pymongo import MongoClient
client = MongoClient()
client.progzoo.authenticate('scott','tiger')
db = client['progzoo']
#FORCE ORDERING
from bson import CodecOptions, SON
opts = CodecOptions(document_class=SON)
db.world = db.world.with_options(codec_options=opts)
#PRETTY
import pprint
pp = pprint.PrettyPrinter(indent=4)

MongoDB uses 'BSON' or 'Binary JSON'. Python interprets BSON data as a dict. Dicts do not retain key order, so SON is added to created an ordered mapping.

By default, find() returns the entire contents of a collection. This is equivalent to find({})

Show all the documents in world

pp.pprint(list(
    db.world.find()
))

pp.pprint(list(db.world.find()))

It is also possible to just return the first document with find_one(). The Mongo shell equivalent to this is findOne()

list() is a python function and is a convient way to display a cursor object. Alternatively you could use a for loop:

for document in db.<collection>.find():
    print(document)

find_one() returns a single document, so a list() or loop is not needed.

Show the first document of world

pp.pprint(db.world.find_one())

pp.pprint(db.world.find_one())

It is also possible to specify which document you want by its position.

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

Get the 50th document of world

pp.pprint(
    db.world.find()[50]
)

pp.print(db.world.find()[50])

Querying

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

Get all the data concerning france

pp.pprint(list(
    db.world.find({"name":"France"})
))

pp.pprint(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

pp.pprint(list(
    db.world.find({"name":"Germany"},{"population":1,"_id":0})
))

pp.pprint(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.

pp.pprint(list(
    db.world.find({"population":{"$lt":1000000}},{"name":1,"_id":0})
))

pp.pprint(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

pp.pprint(list(
    db.world.find({"population":{"$lt":1000000},"area":{"$gt":200000}},{"name":1,"_id":0})
))

pp.pprint(list(db.world.find({"population":{"$lt":1000000},"area":{"$gt":200000}},{"name":1,"_id":0})))

We can also use lists with $in and $nin:

Find the continent of Brazil, the United Kingdom, and Ghana.

pp.pprint(list(
    db.world.find({"name":{"$in":["Brazil","United Kingdom","Ghana"]}},{"name":1,"_id":0})
))

pp.pprint(list(db.world.find({"name":{"$in":["Brazil","United Kingdom","Ghana"]}},{"name":1,"_id":0})))