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
(Created page with "==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 collec...")
 
Line 1: Line 1:
 
==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>
 +
  <pre>
 +
  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
 +
  </pre>
 +
 
<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">
This example shows how to find the population of a country by searching it's name then limiting what fields are returned.<div class="hint" title="SQL Equivalent">
+
By default, find() returns the entire contents of a collection. <i>This is equivalent to find({})</i>
 +
<p class=strong>Show all the documents in world</p>
 +
<pre class=def>
 +
from pymongo import MongoClient
 +
client = MongoClient()
 +
client.progzoo.authenticate('scott','tiger')
 +
db = client['progzoo']
 +
 
 +
print list(db.world.find())
 +
</pre>
 +
<div class=ans>
 +
from pymongo import MongoClient
 +
client = MongoClient()
 +
client.progzoo.authenticate('scott','tiger')
 +
db = client['progzoo']
 +
 
 +
print list(db.world.find({}))
 +
</div>
 +
</div>
 +
 
 +
<div class=q data-lang="py">
 +
It is also possible to just return the first document with find_one(). The Mongo shell the equivalent to this is findOne()<br/>To make things easier to understand the first document of world has been made a list of keys used in these examples.
 +
<p class=strong>Show the first document of world</p>
 +
<pre class=def>
 +
from pymongo import MongoClient
 +
client = MongoClient()
 +
client.progzoo.authenticate('scott','tiger')
 +
db = client['progzoo']
 +
 
 +
print list(db.world.find_one())
 +
</pre>
 +
<div class=ans>
 +
from pymongo import MongoClient
 +
client = MongoClient()
 +
client.progzoo.authenticate('scott','tiger')
 +
db = client['progzoo']
 +
 
 +
print list(db.world.find_one())
 +
</div>
 +
</div>
 +
 
 +
<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
 +
<p class=strong>Show the second document of world</p>
 +
<pre class=def>
 +
from pymongo import MongoClient
 +
client = MongoClient()
 +
client.progzoo.authenticate('scott','tiger')
 +
db = client['progzoo']
 +
 
 +
print list(db.world.find().skip(1).limit(1))
 +
</pre>
 +
<div class=ans>
 +
from pymongo import MongoClient
 +
client = MongoClient()
 +
client.progzoo.authenticate('scott','tiger')
 +
db = client['progzoo']
 +
 
 +
print list(db.world.find().skip(1).limit(1))
 +
</div>
 +
</div>
 +
 
 +
<div class=q data-lang="py">
 +
What if we want the last document in a collection? For this we can use count()<br/>
 +
Conveniently, db.collection.find().count() and db.collection.count() are identical.
 +
<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>
 +
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))
 +
</pre>
 +
<div class=ans>
 +
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))
 +
</div>
 +
</div>
 +
==Querying==
 +
<div class=q data-lang="py">
 +
By passing arguments to find() we can search for specific documents
 +
<p class=strong>Get all the data concerning france</p>
 +
<div class="hint" title="SQL Equivalent">
 
<pre>
 
<pre>
  SELECT population
+
SELECT *
  FROM world  
+
FROM world
  WHERE name = 'France'
+
WHERE name = 'France'
 
</pre>
 
</pre>
 
</div>
 
</div>
<p class=strong>Show the population of France</p>
 
 
<pre class=def>
 
<pre class=def>
from pymongo import MongoClient             # Import pymongo
+
from pymongo import MongoClient
client = MongoClient()                      # Use the default client settings ('mongodb://localhost:27017/')
+
client = MongoClient()
client.progzoo.authenticate('scott','tiger') # Log in as "Scott" with the password "Tiger"
+
client.progzoo.authenticate('scott','tiger')
db = client['progzoo']                       # Use the "progzoo" database
+
db = client['progzoo']
  
print list(db.world.find({"name":"France"},{"population":1, "_id":0}))
+
print list(db.world.find({"name":"France"}))
 
</pre>
 
</pre>
 
<div class=ans>
 
<div class=ans>
console.log("Hello World");
+
from pymongo import MongoClient
 +
client = MongoClient()
 +
client.progzoo.authenticate('scott','tiger')
 +
db = client['progzoo']
 +
 
 +
print list(db.world.find({"name":"France"}))
 +
</div>
 +
</div>
 +
 
 +
<div class=q data-lang="py">
 +
By passing a second parameter to find() the output can be limited to certain field(s)<br/>
 +
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>
 +
SELECT population
 +
FROM world
 +
WHERE name = 'Germany'
 +
</pre>
 +
</div>
 +
<pre class=def>
 +
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}))
 +
</pre>
 +
<div class=ans>
 +
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}))
 
</div>
 
</div>
 
</div>
 
</div>

Revision as of 13:09, 14 July 2015

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}))