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 1: Line 1:
 +
<div
 
==Introducing the <code>world</code> collection of countries==
 
==Introducing the <code>world</code> collection of countries==
 
   <p>These examples introduce NoSQL using MonogDB and PyMongo. We will be using the find() command on the collection '''world''':</p>
 
   <p>These examples introduce NoSQL using MonogDB and PyMongo. We will be using the find() command on the collection '''world''':</p>
   <p>A note about the setup</p>
+
  <div class="hint" title="Techincal details">
 +
   <p>The following is included in the examples but hidden</p>
 
   <pre>
 
   <pre>
 
   from pymongo import MongoClient              # Import the pymongo package
 
   from pymongo import MongoClient              # Import the pymongo package
   client = MongoClient()                      # Use thee default client value('mongodb://localhost:27017/')
+
   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
 
   client.progzoo.authenticate('scott','tiger') # Log in using the username scott and the password tiger
 
   db = client['progzoo']                      # Use the 'progzoo' database
 
   db = client['progzoo']                      # Use the 'progzoo' database
 
   </pre>
 
   </pre>
 
+
  </div>
 
<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="py">
 
<div class=q data-lang="py">
Line 40: Line 42:
 
db = client['progzoo']
 
db = client['progzoo']
  
print list(db.world.find_one())
+
print db.world.find_one()
 
</pre>
 
</pre>
 
<div class=ans>
 
<div class=ans>
Line 48: Line 50:
 
db = client['progzoo']
 
db = client['progzoo']
  
print list(db.world.find_one())
+
print db.world.find_one()
 
</div>
 
</div>
 
</div>
 
</div>
  
 
<div class=q data-lang="py">
 
<div class=q data-lang="py">
Let's return the second document by skipping the first one and limiting our find to just one result
+
It is also possible to skip documents and limit the amount you return.
<p class=strong>Show the second document of world</p>
+
<p class=strong>Get the 50th document of world</p>
 
<pre class=def>
 
<pre class=def>
 
from pymongo import MongoClient
 
from pymongo import MongoClient
Line 61: Line 63:
 
db = client['progzoo']
 
db = client['progzoo']
  
print list(db.world.find().skip(1).limit(1))
+
print list(db.world.find().skip(49).limit(1))
 
</pre>
 
</pre>
 
<div class=ans>
 
<div class=ans>
Line 73: Line 75:
 
</div>
 
</div>
  
 +
==Querying==
 
<div class=q data-lang="py">
 
<div class=q data-lang="py">
What if we want the last document in a collection? For this we can use count()<br/>
+
By passing arguments to find() we can search for specific documents
Conveniently, db.collection.find().count() and db.collection.count() are identical.
+
<p class=strong>Get all the data concerning france</p>
<p class=strong>Show the amount of documents in the collection and the last document</p>
 
<div class="hint" title="Why and when to use list">
 
find() returns a [http://api.mongodb.org/python/current/api/pymongo/cursor.html cursor object]. A way to see the contents is to wrap a list. You can avoid this by using Mongo shell
 
</div>
 
 
<pre class=def>
 
<pre class=def>
 
from pymongo import MongoClient
 
from pymongo import MongoClient
Line 86: Line 85:
 
db = client['progzoo']
 
db = client['progzoo']
  
print db.world.count()
+
print list(db.world.find({"name":"France"}))
print list(db.world.find().skip(db.world.count()-1))
 
 
</pre>
 
</pre>
 
<div class=ans>
 
<div class=ans>
Line 95: Line 93:
 
db = client['progzoo']
 
db = client['progzoo']
  
print db.world.count()
+
print list(db.world.find({"name":"France"}))
print list(db.world.find().skip(db.world.count()-1))
 
 
</div>
 
</div>
 
</div>
 
</div>
==Querying==
+
 
 
<div class=q data-lang="py">
 
<div class=q data-lang="py">
By passing arguments to find() we can search for specific documents
+
By passing a second parameter to find() the output can be limited to certain field(s)<br/>
<p class=strong>Get all the data concerning france</p>
+
In this example 1 indicates "true" and 0 indicates "false"<br /><br/>
<div class="hint" title="SQL Equivalent">
+
A feature of MongoDB is the [http://docs.mongodb.org/manual/reference/object-id/ ObjectID] or "_id".<br/>  
<pre>
+
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/>
SELECT *
+
<p class=strong>Get the population of Germany</p>
FROM world
 
WHERE name = 'France'
 
</pre>
 
</div>
 
 
<pre class=def>
 
<pre class=def>
 
from pymongo import MongoClient
 
from pymongo import MongoClient
Line 116: Line 109:
 
db = client['progzoo']
 
db = client['progzoo']
  
print list(db.world.find({"name":"France"}))
+
print list(db.world.find({"name":"Germany"},{"population":1,"_id":0}))
 
</pre>
 
</pre>
 
<div class=ans>
 
<div class=ans>
Line 124: Line 117:
 
db = client['progzoo']
 
db = client['progzoo']
  
print list(db.world.find({"name":"France"}))
+
print list(db.world.find({"name":"Germany"},{"population":1,"_id":0}))
 
</div>
 
</div>
 
</div>
 
</div>
  
 
<div class=q data-lang="py">
 
<div class=q data-lang="py">
By passing a second parameter to find() the output can be limited to certain field(s)<br/>
+
MongoDB also allows comparisons. Syntax:
In this example 1 indicates "true" and 0 indicates "false"<br /><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.
 
<p class=strong>Get the population of Germany</p>
 
<div class="hint" title="SQL Equivalent">
 
 
<pre>
 
<pre>
SELECT population
+
Mongo | MySQL
FROM world
+
--------------
WHERE name = 'Germany'
+
$eq  | ==
 +
$gt  | >
 +
$gte  | >=
 +
$lt  | <
 +
$lte  | <=
 +
$ne  | !=, <>
 +
$in  | IN
 +
$nin  | NOT IN
 
</pre>
 
</pre>
</div>
+
<p class=strong>List the countries with a population that's less than 1 million.</p>
 
<pre class=def>
 
<pre class=def>
 
from pymongo import MongoClient
 
from pymongo import MongoClient
Line 147: Line 142:
 
db = client['progzoo']
 
db = client['progzoo']
  
print list(db.world.find({"name":"France"},{"population":1,"_id":0}))
+
print list(db.world.find({"population":{"$lt":1000000}},{"name":1,"_id":0}))
 
</pre>
 
</pre>
 
<div class=ans>
 
<div class=ans>
Line 155: Line 150:
 
db = client['progzoo']
 
db = client['progzoo']
  
print list(db.world.find({"name":"France"},{"population":1,"_id":0}))
+
print list(db.world.find({"population":{"$lt":1000000}},{"name":1,"_id":0}))
 
</div>
 
</div>
 
</div>
 
</div>

Revision as of 15:22, 14 July 2015

<div

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.

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(1).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}))