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 98: Line 98:
 
<p class=strong>Find the countries with less than 1 million people, but over 200000km<sup>2</sup> area</p>
 
<p class=strong>Find the countries with less than 1 million people, but over 200000km<sup>2</sup> area</p>
 
<pre class=def>
 
<pre class=def>
pp.pprint(list(
+
 
 
     db.world.find({"population":{"$lt":1000000},"area":{"$gt":200000}},{"name":1,"_id":0})
 
     db.world.find({"population":{"$lt":1000000},"area":{"$gt":200000}},{"name":1,"_id":0})
))
+
 
 
</pre>
 
</pre>
 
<div class=ans>
 
<div class=ans>
pp.pprint(list(db.world.find({"population":{"$lt":1000000},"area":{"$gt":200000}},{"name":1,"_id":0})))
+
db.world.find({"population":{"$lt":1000000},"area":{"$gt":200000}},{"name":1,"_id":0})
 
</div>
 
</div>
 
</div>
 
</div>

Revision as of 08:12, 28 October 2016

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

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

Show the first document of world

db.world.findOne()

db.world.findOne()

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]

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

db.world.findOne({"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

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

db.world.find({"population":{"$lt":1000000}},{"name":1,"_id":0})
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


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

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.

db.world.find({"population":{"$lt":1000000},"area":{"$gt":200000}},{"name":1,"_id":0})
db.world.find({"population":{"$lt":1000000},"area":{"$gt":200000}},{"name":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

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

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