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 36: Line 36:
 
<div class='extra_space' style='width:1em; height:6em;'></div>
 
<div class='extra_space' style='width:1em; height:6em;'></div>
 
<div class=q data-lang="py3">
 
<div class=q data-lang="py3">
By default, find() returns the entire contents of a collection. <i>This is equivalent to find({})</i>
+
By default, find() returns the entire contents of a collection. This is equivalent to find({})
 
<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>
Line 68: Line 68:
  
 
<div class=q data-lang="py3">
 
<div class=q data-lang="py3">
It is also possible to specify which document you want by its position.
+
It is also possible to specify which document to find by its position.
 
<div class="hint" title="How to achieve the same with skip() and limit()">
 
<div class="hint" title="How to achieve the same with skip() and limit()">
 
<pre>
 
<pre>
Line 89: Line 89:
 
==Querying==
 
==Querying==
 
<div class=q data-lang="py3">
 
<div class=q data-lang="py3">
By passing arguments to find() we can search for specific documents
+
<code>find()</code> can filter results if a document is used as an argument.
 
<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>
Line 102: Line 102:
  
 
<div class=q data-lang="py3">
 
<div class=q data-lang="py3">
By passing a second parameter to find() the output can be limited to certain field(s)<br/>
+
By passing a second parameter to <code>find()</code> the output can be limited to certain field(s)<br/>
In this example 1 indicates "true" and 0 indicates "false"<br /><br/>
+
In this example 1 indicates "true" and 0 indicates "false"<br />
 +
The arguments don't need to be in any particular order<br/><br/>
 
A feature of MongoDB is the [http://docs.mongodb.org/manual/reference/object-id/ ObjectID] or "_id".<br/>  
 
A feature of MongoDB is the [http://docs.mongodb.org/manual/reference/object-id/ ObjectID] or "_id".<br/>  
 
This is a unique ID MongoDB adds to each document. Unlike other keys, it has to be <b>explicitly</b> set to false to be excluded from the results set.<br/>
 
This is a unique ID MongoDB adds to each document. Unlike other keys, it has to be <b>explicitly</b> set to false to be excluded from the results set.<br/>
Line 169: Line 170:
  
 
<div class=q data-lang="py3">
 
<div class=q data-lang="py3">
We can also use lists with $in and $nin:
+
Lists can be used with <code>$in</code> and <code>$nin</code>:
 
<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>
Line 182: Line 183:
  
 
<div class=q data-lang="py3">
 
<div class=q data-lang="py3">
We can perform pattern matching with Regular Expressions (RegEx)<br />
+
Pattern matching is possible with Regular Expressions (RegEx)<br />
 
The Mongo shell syntax is simpler than pymongo:<br/>
 
The Mongo shell syntax is simpler than pymongo:<br/>
 
<code>db.<collection>.find({<field>:/.*/})</code>
 
<code>db.<collection>.find({<field>:/.*/})</code>

Revision as of 12:15, 22 July 2015

#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']
#PRETTY
import pprint
pp = pprint.PrettyPrinter(indent=4)

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']
#PRETTY
import pprint
pp = pprint.PrettyPrinter(indent=4)

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 to find 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.pprint(db.world.find()[50])

Querying

find() can filter results if a document is used as an argument.

Get all the data concerning france

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

pp.pprint(db.world.find_one({"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"
The arguments don't need to be in any particular order

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(
    db.world.find_one({"name":"Germany"},{"population":1,"_id":0})
)

pp.pprint(db.world.find_one({"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 use in an $and, $or, etc. This can be done in several ways, for example:

AND (implicit)
db.<collection>.find({<first condition>,<second condition>}
db.world.find({"population":{"$lt":1000000},"area":{"$gt":200000})

AND (explicit)
db.<collection>.find({"$and":[<first condition>,<second condition>]}
db.world.find({"$and":[{"population":{"$lt":1000000}},{"area":{"$gt":200000}}]})

OR
db.<collection>.find({"$or":[<first condition>,<second condition>]}
db.world.find({"$or":[{"population":{"$lt":1000000}},{"area":{"$gt":200000}}]})

Find the countries 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})))

Lists can be used 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,"continent":1,"_id":0})
))

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

Pattern matching is possible with Regular Expressions (RegEx)
The Mongo shell syntax is simpler than pymongo:
db.<collection>.find({<field>:/.*/})

Show each country that begins with G

pp.pprint(list(
    db.world.find({"name":{'$regex':"^G"}},{"name":1,"_id":0})
))

pp.pprint(list(db.world.find({"name":{'$regex':"^G"}},{"name":1,"_id":0})))