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
(Fix bug and tidy.)
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 MongoDB. We will be using the find() command on the collection '''world''':</p>
 
<p>These examples introduce NoSQL using MongoDB. We will be using the find() command on the collection '''world''':</p>
<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=mongo>
+
<div class="q" data-lang="mongo">
 
By default, <code>find()</code> returns the entire contents of a collection. This is equivalent to <code>find({})</code>
 
By default, <code>find()</code> returns the entire contents of a collection. This is equivalent to <code>find({})</code>
<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">db.world.find()</pre>
db.world.find()
 
</pre>
 
 
</div>
 
</div>
  
 
==find and findOne==
 
==find and findOne==
<div class=q data-lang=mongo>
+
<div class="q" data-lang="mongo">
 
It is also possible to just return the first document with <code>findOne()</code>.
 
It is also possible to just return the first document with <code>findOne()</code>.
<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">db.world.findOne()</pre>
db.world.findOne()
 
</pre>
 
<div class=ans>
 
db.world.findOne()
 
</div>
 
 
</div>
 
</div>
  
 
==skip and limit==
 
==skip and limit==
<div class=q data-lang="mongo">
+
<div class="q" data-lang="mongo">
 
It is also possible to specify which document to find by its position.
 
It is also possible to specify which document to find by its position.
 
<div>You can achieve the same with skip() and limit()</div>
 
<div>You can achieve the same with skip() and limit()</div>
Line 29: Line 22:
 
db.world.find().skip(49).limit(1)
 
db.world.find().skip(49).limit(1)
 
</pre>
 
</pre>
<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">db.world.find()[49]</pre>
db.world.find()[49]
 
</pre>
 
<div class=ans>
 
db.world.find()[49]
 
</div>
 
 
</div>
 
</div>
  
 
==Querying==
 
==Querying==
<div class=q data-lang="mongo">
+
<div class="q" data-lang="mongo">
 
<code>find()</code> can filter results if a document is used as an argument.  
 
<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">db.world.findOne({"name":"France"})</pre>
db.world.findOne({"name":"France"})
 
</pre>
 
<div class=ans>
 
db.world.findOne({"name":"France"})
 
</div>
 
 
</div>
 
</div>
  
 
==Using a projection with find==
 
==Using a projection with find==
<div class=q data-lang="mongo">
+
<div class="q" data-lang="mongo">
 
By passing a second parameter to <code>find()</code> 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/><br/>
 
A feature of MongoDB is the [http://docs.mongodb.org/manual/reference/object-id/ ObjectID] or <code>_id</code>.<br/>  
 
A feature of MongoDB is the [http://docs.mongodb.org/manual/reference/object-id/ ObjectID] or <code>_id</code>.<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">db.world.findOne({"name":"Germany"},{"population":1,"_id":0})</pre>
db.world.findOne({"name":"Germany"},{"population":1,"_id":0})</pre>
 
<div class=ans>
 
db.world.findOne({"name":"Germany"},{"population":1,"_id":0})</div>
 
 
</div>
 
</div>
  
 
==Comparisons $eq %gt $gte $lte $ne==
 
==Comparisons $eq %gt $gte $lte $ne==
<div class=q data-lang="mongo">
+
<div class="q" data-lang="mongo">
 
MongoDB also allows comparisons. Syntax:
 
MongoDB also allows comparisons. Syntax:
 
<pre>
 
<pre>
Line 78: Line 58:
 
$nin  | NOT IN
 
$nin  | NOT IN
 
</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">db.world.find({"population":{"$lt":1000000}},{"name":1,"_id":0})</pre>
db.world.find({"population":{"$lt":1000000}},{"name":1,"_id":0})</pre>
 
<div class=ans>
 
db.world.find({"population":{"$lt":1000000}},{"name":1,"_id":0})</div>
 
 
</div>
 
</div>
  
 
==Logical operations $and $or $not==
 
==Logical operations $and $or $not==
<div class=q data-lang="mongo">
+
<div class="q" data-lang="mongo">
It's also possible to have multiple conditions for use in an $and, $or, etc. This can be done in several ways, for example:
+
It's also possible to have multiple conditions for use in an <code>$and</code>, <code>$or</code>, etc. This can be done in several ways, for example:
 
<pre>
 
<pre>
 
AND (implicit)
 
AND (implicit)
Line 101: Line 78:
 
db.world.find({"$or":[{"population":{"$lt":1000000}},{"area":{"$gt":200000}}]})
 
db.world.find({"$or":[{"population":{"$lt":1000000}},{"area":{"$gt":200000}}]})
 
</pre>
 
</pre>
<p class=strong>Find the countries with less than 1 million people, but over 200000km<sup>2</sup> area</p>
 
<pre class=def>
 
  
    db.world.find({"population":{"$lt":1000000},"area":{"$gt":200000}},{"name":1,"_id":0})
+
<p class="strong">Find the countries with less than 1 million people, but over 200000km<sup>2</sup> area.</p>
 
+
<pre class="def">db.world.find({"population":{"$lt":1000000},"area":{"$gt":200000}},{"name":1,"_id":0})</pre>
</pre>
 
<div class=ans>
 
db.world.find({"population":{"$lt":1000000},"area":{"$gt":200000}},{"name":1,"_id":0})
 
</div>
 
 
</div>
 
</div>
  
 
==$in and $nin==
 
==$in and $nin==
<div class=q data-lang="mongo">
+
<div class="q" data-lang="mongo">
 
Lists can be used with <code>$in</code> and <code>$nin</code>:
 
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">
 
db.world.find(
 
db.world.find(
 
   {name:{$in:['Brazil','Ghana','United Kingdom']}},
 
   {name:{$in:['Brazil','Ghana','United Kingdom']}},
 
   {name:1,continent:1,_id:0}
 
   {name:1,continent:1,_id:0}
)</pre>
+
)
<div class=ans>
+
</pre>
db.world.find(
 
  {name:{$in:['Brazil','Ghana','United Kingdom']}},
 
  {name:1,continent:1,_id:0}
 
)</div>
 
 
</div>
 
</div>
  
 
==Regular Expressions==
 
==Regular Expressions==
<div class=q data-lang="mongo">
+
<div class="q" data-lang="mongo">
Pattern matching is possible with Regular Expressions (RegEx)<br />
+
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>
[[https://docs.mongodb.com/manual/reference/operator/query/regex/]]
+
[[RegEx_Pattern_Matching]]
<pre class=def>
+
<pre class="def">
 
db.world.find(
 
db.world.find(
 
   {name:{$regex:/^G/}},
 
   {name:{$regex:/^G/}},
Line 139: Line 106:
 
)
 
)
 
</pre>
 
</pre>
<div class=ans>
 
db.world.find(
 
  {name:{$regex:/^G/}},
 
  {name:1,continent:1,_id:0}
 
)
 
</div>
 
 
</div>
 
</div>

Revision as of 00:23, 9 April 2018

Introducing the world collection of countries

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

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 specify which document to find by its position.

You can achieve the same with skip() and limit()
db.world.find().skip(49).limit(1)

Get the 50th document of world

db.world.find()[49]

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 $eq %gt $gte $lte $ne

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