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
 
(48 intermediate revisions by 6 users not shown)
Line 1: Line 1:
 
+
==Introducing the '''world''' collection of countries==
==Introducing the <code>world</code> collection of countries==
+
<p>These examples introduce NoSQL using MongoDB. We will be using the <syntaxhighlight lang="JavaScript" inline>find()</syntaxhighlight> command on the '''world''' collection .</p>
  <p>These examples introduce NoSQL using MonogDB and PyMongo under Python3.4. We will be using the find() command on the collection '''world''':</p>
+
<div class="q" data-lang="mongo">
  <div class="hint" title="Techincal details">
+
By default, <syntaxhighlight lang="JavaScript" inline>find()</syntaxhighlight> returns the entire contents of a collection. This is equivalent to <syntaxhighlight lang="JavaScript" inline>find({})</syntaxhighlight>
  <p>The following is included in the examples but hidden</p>
+
<p class="strong">Show all the documents in world</p>
<pre class=setup>
+
<pre class="def"><nowiki>db.world.find();</nowiki></pre>
#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)
 
</pre>
 
  </div>
 
 
 
<div class='extra_space' style='width:1em; height:6em;'></div>
 
<div class=q data-lang="py3">
 
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>
 
pp.pprint(list(
 
    db.world.find()
 
))
 
</pre>
 
<div class=ans>
 
pp.pprint(list(db.world.find()))
 
</div>
 
 
</div>
 
</div>
  
<div class=q data-lang="py3">
+
==find and findOne==
It is also possible to just return the first document with find_one(). The Mongo shell equivalent to this is findOne()
+
<div class="q" data-lang="mongo">
<div class="hint" title="When to use list()">
+
It is also possible to just return the first document with <syntaxhighlight lang="JavaScript" inline>findOne()</syntaxhighlight>.
list() is a python function and is a convient way to display a [http://api.mongodb.org/python/current/api/pymongo/cursor.html cursor object]. Alternatively you could use a for loop:
+
<p class="strong">Show the first document of world</p>
<pre>
+
<pre class="def"><nowiki>db.world.findOne();</nowiki></pre>
for document in db.<collection>.find():
 
    print(document)
 
</pre>
 
find_one() returns a single document, so a list() or loop is not needed.
 
</div>
 
<p class=strong>Show the first document of world</p>
 
<pre class=def>
 
pp.pprint(db.world.find_one())
 
</pre>
 
<div class=ans>
 
pp.pprint(db.world.find_one())
 
</div>
 
 
</div>
 
</div>
  
<div class=q data-lang="py3">
+
==skip and limit==
It is also possible to specify which document you want by its position.
+
<div class="q" data-lang="mongo">
<div class="hint" title="How to achieve the same with skip() and limit()">
+
It is also possible to retrieve a specific document based on its position using <syntaxhighlight lang="JavaScript" inline>skip(n-1)</syntaxhighlight> and <syntaxhighlight lang="JavaScript" inline>limit(1)</syntaxhighlight>.<br/>
<pre>
+
As this is JavaScript you can also access by index: <syntaxhighlight lang="JavaScript" inline>db.world.find()[n-1];</syntaxhighlight>
print(list(
+
<p class="strong">Get the 50th document of world</p>
    db.world.find().skip(49).limit(1)
+
<pre class="def"><nowiki>db.world.find().skip(49).limit(1);</nowiki></pre>
))
 
</pre>
 
</div>
 
<p class=strong>Get the 50th document of world</p>
 
<pre class=def>
 
pp.pprint(
 
    db.world.find()[50]
 
)
 
</pre>
 
<div class=ans>
 
pp.pprint(db.world.find()[50])
 
</div>
 
 
</div>
 
</div>
  
 
==Querying==
 
==Querying==
<div class=q data-lang="py3">
+
<div class="q" data-lang="mongo">
By passing arguments to find() we can search for specific documents
+
<syntaxhighlight lang="JavaScript" inline>find()</syntaxhighlight> 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"><nowiki>db.world.findOne({name: "France"});</nowiki></pre>
pp.pprint(
 
    db.world.find_one({"name":"France"})
 
)
 
</pre>
 
<div class=ans>
 
pp.pprint(db.world.find_one({"name":"France"}))
 
</div>
 
 
</div>
 
</div>
  
<div class=q data-lang="py3">
+
==Using a projection with find==
By passing a second parameter to find() the output can be limited to certain field(s)<br/>
+
<div class="q" data-lang="mongo">
In this example 1 indicates "true" and 0 indicates "false"<br /><br/>
+
By passing a second parameter to <syntaxhighlight lang="JavaScript" inline>find()</syntaxhighlight> the output can be limited to certain field(s)<br/>
A feature of MongoDB is the [http://docs.mongodb.org/manual/reference/object-id/ ObjectID] or "_id".<br/>  
+
In this example '''1''' indicates <syntaxhighlight lang="JavaScript" inline>true</syntaxhighlight> and '''0''' indicates <syntaxhighlight lang="JavaScript" inline>false</syntaxhighlight><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 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/>
<p class=strong>Get the population of Germany</p>
+
<p class="strong">Get the population of Germany</p>
<pre class=def>
+
<pre class="def"><nowiki>db.world.findOne({name: "Germany"}, {population: 1, _id: 0});</nowiki></pre>
pp.pprint(
 
    db.world.find_one({"name":"Germany"},{"population":1,"_id":0})
 
)
 
</pre>
 
<div class=ans>
 
pp.pprint(db.world.find_one({"name":"Germany"},{"population":1,"_id":0}))
 
</div>
 
 
</div>
 
</div>
  
<div class=q data-lang="py3">
+
==Comparisons==
 +
<div class="q" data-lang="mongo">
 
MongoDB also allows comparisons. Syntax:
 
MongoDB also allows comparisons. Syntax:
 
<pre>
 
<pre>
 +
<nowiki>
 
Mongo | MySQL
 
Mongo | MySQL
 
--------------
 
--------------
Line 116: Line 54:
 
$in  | IN
 
$in  | IN
 
$nin  | NOT IN
 
$nin  | NOT IN
 +
</nowiki>
 
</pre>
 
</pre>
<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"><nowiki>db.world.find({population: {$lt: 1000000}}, {name: 1, _id: 0});</nowiki></pre>
pp.pprint(list(
 
    db.world.find({"population":{"$lt":1000000}},{"name":1,"_id":0})
 
))
 
</pre>
 
<div class=ans>
 
pp.pprint(list(db.world.find({"population":{"$lt":1000000}},{"name":1,"_id":0})))
 
</div>
 
 
</div>
 
</div>
  
<div class=q data-lang="py3">
+
==Logical operations $and $or $not==
It's also possible to have multiple conditions for use in an $and, $or, etc. This can be done in several ways, for example:
+
<div class="q" data-lang="mongo">
<pre>
+
It's also possible to have multiple conditions for use in an '''$and''', '''$or''', etc. This can be done in several ways, for example:
db.<collection>.find({<first condition>,<second condition>}
+
<syntaxhighlight lang="JavaScript">
db.world.find({"population":{"$lt":1000000},"area":{"$gt":200000})
+
// 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}}]});
  
db.<collection>.find({"$and":[<first condition>,<second condition>]}
+
// OR
db.world.find({"$and":[{"population":{"$lt":1000000}},{"area":{"$gt":200000}}]}
+
db.<collection>.find({$or: [<first condition>, <second condition>]});
</pre>
+
db.world.find({$or: [{population: {$lt: 1000000}}, {area: {$gt: 200000}}]});
<p class=strong>Find the countries with less than 1 million people, but over 200000km<sup>2</sup> area</p>
+
</syntaxhighlight>
<pre class=def>
 
pp.pprint(list(
 
    db.world.find({"population":{"$lt":1000000},"area":{"$gt":200000}},{"name":1,"_id":0})
 
))
 
</pre>
 
<div class=ans>
 
pp.pprint(list(db.world.find({"population":{"$lt":1000000},"area":{"$gt":200000}},{"name":1,"_id":0})))
 
</div>
 
</div>
 
  
<div class=q data-lang="py3">
+
<p class="strong">Find the countries with less than 1 million people, but over 200000km<sup>2</sup> area.</p>
We can also use lists with $in and $nin:
+
<pre class="def"><nowiki>db.world.find({population: {$lt: 1000000}, area: {$gt: 200000}}, {name: 1, _id: 0});</nowiki></pre>
<p class=strong>Find the continent of Brazil, the United Kingdom, and Ghana.</p>
 
<pre class=def>
 
pp.pprint(list(
 
    db.world.find({"name":{"$in":["Brazil","United Kingdom","Ghana"]}},{"name":1,"continent":1,"_id":0})
 
))
 
</pre>
 
<div class=ans>
 
pp.pprint(list(db.world.find({"name":{"$in":["Brazil","United Kingdom","Ghana"]}},{"name":1,"continent":1,"_id":0})))
 
</div>
 
 
</div>
 
</div>
  
 
+
==$in and $nin==
<div class=q data-lang="py3">
+
<div class="q" data-lang="mongo">
We can also use lists with $in and $nin:
+
Lists can be used with '''$in''' and '''$nin''':
<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"><nowiki>
pp.pprint(list(
+
db.world.find(
    db.world.find({"name":{"$in":["Brazil","United Kingdom","Ghana"]}},{"name":1,"continent":1,"_id":0})
+
  {name: {$in: ['Brazil', 'Ghana', 'United Kingdom']}},
))
+
  {name: 1, continent: 1, _id: 0}
</pre>
+
);</nowiki></pre>
<div class=ans>
 
pp.pprint(list(db.world.find({"name":{"$in":["Brazil","United Kingdom","Ghana"]}},{"name":1,"continent":1,"_id":0})))
 
</div>
 
 
</div>
 
</div>
  
 
+
==Regular Expressions==
<div class=q data-lang="py3">
+
<div class="q" data-lang="mongo">
We can perform pattern matching with Regular Expressions (RegEx)
+
Pattern matching is possible with Regular Expressions (RegEx)<br/>
<p class=strong>Show each country that begins with G</p>
+
<p class="strong">Show each country that begins with G.</p> [[RegEx_Pattern_Matching | RegEx Pattern Matching]]
<pre class=def>
+
<pre class="def"><nowiki>
pp.pprint(list(
+
db.world.find(
    db.world.find({"name":{'$regex':^G}},{"name":1,"_id":0})
+
  {name: {$regex: /^G/}},
))
+
  {name: 1, continent: 1, _id: 0}
</pre>
+
);</nowiki></pre>
<div class=ans>
 
pp.pprint(list(db.world.find({"name": /^G/},{"name":1,"_id":0})))
 
</div>
 
 
</div>
 
</div>

Latest revision as of 14:12, 17 October 2018

Introducing the world collection of countries

These examples introduce NoSQL using MongoDB. We will be using the find() command on the world collection .

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

Show all the documents in world

db.world.find();

find and findOne

It is also possible to just return the first document with findOne().

Show the first document of world

db.world.findOne();

skip and limit

It is also possible to retrieve a specific document based on its position using skip(n-1) and limit(1).
As this is JavaScript you can also access by index: db.world.find()[n-1];

Get the 50th document of world

db.world.find().skip(49).limit(1);

Querying

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

Get all the data concerning france

db.world.findOne({name: "France"});

Using a projection with find

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

db.world.findOne({name: "Germany"}, {population: 1, _id: 0});

Comparisons

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.

db.world.find({population: {$lt: 1000000}}, {name: 1, _id: 0});

Logical operations $and $or $not

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.

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

$in and $nin

Lists can be used with $in and $nin:

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

db.world.find(
   {name: {$in: ['Brazil', 'Ghana', 'United Kingdom']}},
   {name: 1, continent: 1, _id: 0}
);

Regular Expressions

Pattern matching is possible with Regular Expressions (RegEx)

Show each country that begins with G.

RegEx Pattern Matching
db.world.find(
  {name: {$regex: /^G/}},
  {name: 1, continent: 1, _id: 0}
);