MongoDB – Commonly used queries in big data – Part 1 of 4

December 19, 2012
By

This article is a continuation to the article titled “MongoDB Basics – A Quick Start Tutorial“, where we have created a database called “university” and a collection namely “students” with some data in it.

In this article we will see some of the commonly used MongoDB query statements to fetch data from the “students” collection.

All the MongoDB statements given in this article are tested in MongoDB shell version 2.2.2.


Open database and fetch all the existing data from “students” collections?

>use university
>db.students.find()
{ "_id" : ObjectId("50cebdcc984024d0901bad60"), "name" : "Rajesh", "age" : 21, "gender" : "M", "course" : "BTECH", "marks" : 81 }
{ "_id" : ObjectId("50cebe81984024d0901bad61"), "name" : "John", "age" : 21, "gender" : "M", "course" : "BTECH", "marks" : 79 }
{ "_id" : ObjectId("50cebeccb73c3219ce6f4bb4"), "name" : "Firoz", "age" : 22, "gender" : "M", "course" : "BTECH", "marks" : 88 }
{ "_id" : ObjectId("50cebeccb73c3219ce6f4bb5"), "name" : "Alan", "age" : 21, "gender" : "M", "course" : "BTECH", "marks" : 68 }

Retrieving all the existing documents from a collection with selected fields ?

Syntax

db.students.find([query],[fields])

Example : Showing all the documents with the fields name and gender

>db.students.find({},{_id:0,name:1,gender:1});
{ "name" : "Rajesh", "gender" : "M" }
{ "name" : "John", "gender" : "M" }
{ "name" : "Firoz", "gender" : "M" }
{ "name" : "Alan", "gender" : "M" }

Note : By default, the field _id will be shown in the output, though it is not specified in the fields list. So in order to hide it, we need to explicitly specify _id:0 in the field list


Inserting an array of documents into a MongoDB collection?

Syntax

db.students.insert(obj)

Example : Inserting a couple of documents into students collection


> db.students.insert([
... {name:"Anjali",age:20,gender:"F",course:"MBA",marks:67},
... {name:"Ali Khan",age:20,gender:"M",course:"MBA",marks:59}
... ]
... )

SQL’s “where” clause equivalent in MongoDB?

Syntax

db.students.find([query],[fields])

Example : All students belongs to the course “BTECH” is listed

>db.students.find({course:"BTECH"})
{ "_id" : ObjectId("50cd99b62e85f03ba01adafb"), "name" : "Rajesh", "age" : 21, "gender" : "M", "course" : "BTECH", "marks" : 81 }
{ "_id" : ObjectId("50cd99f22e85f03ba01adafc"), "name" : "John", "age" : 21, "gender" : "M", "course" : "BTECH", "marks" : 79 }
{ "_id" : ObjectId("50cd9b38d0262f1b85a3fc57"), "name" : "Firoz", "age" : 22, "gender" : "M", "course" : "BTECH", "marks" : 88 }
{ "_id" : ObjectId("50cd9b38d0262f1b85a3fc58"), "name" : "Alan", "age" : 21, "gender" : "M", "course" : "BTECH", "marks" : 68 }

SQL’s “greater than or equal ( >= ) ” operator equivalent in MongoDB?

Syntax

db.students.find([query],[fields])

Example : Lists all students whose mark is greater than or equal to 80 percentage

>db.students.find({marks:{$gte:80}})
{ "_id" : ObjectId("50cd99b62e85f03ba01adafb"), "name" : "Rajesh", "age" : 21, "gender" : "M", "course" : "BTECH", "marks" : 81 }
{ "_id" : ObjectId("50cd9b38d0262f1b85a3fc57"), "name" : "Firoz", "age" : 22, "gender" : "M", "course" : "BTECH", "marks" : 88 }

SQL’s “not equal to( <> or != ) ” operator equivalent in MongoDB?

Syntax

db.students.find([query],[fields])

Example : Lists all students whose age is not 22

>db.students.find({age:{$ne:22}})
{ "_id" : ObjectId("50cebdcc984024d0901bad60"), "name" : "Rajesh", "age" : 21, "gender" : "M", "course" : "BTECH", "marks" : 81 }
{ "_id" : ObjectId("50cebe81984024d0901bad61"), "name" : "John", "age" : 21, "gender" : "M", "course" : "BTECH", "marks" : 79 }
{ "_id" : ObjectId("50cebeccb73c3219ce6f4bb5"), "name" : "Alan", "age" : 21, "gender" : "M", "course" : "BTECH", "marks" : 68 }

SQL’s “OR” operator equivalent in MongoDB?

Syntax

db.students.find([query],[fields])

Example : Lists all the students whose course is “MBA” or  marks greater than 80 percentage

>db.students.find({$or: [ {course:"MBA"}, {marks:{$gte:80}} ] } )
{ "_id" : ObjectId("50cebdcc984024d0901bad60"), "name" : "Rajesh", "age" : 21, "gender" : "M", "course" : "BTECH", "marks" : 81 }
{ "_id" : ObjectId("50cebeccb73c3219ce6f4bb4"), "name" : "Firoz", "age" : 22, "gender" : "M", "course" : "BTECH", "marks" : 88 }
{ "_id" : ObjectId("50d09dd394fd058d725d1853"), "name" : "Anjali", "age" : 20, "gender" : "F", "course" : "MBA", "marks" : 67 }
{ "_id" : ObjectId("50d09dd394fd058d725d1854"), "name" : "Ali Khan", "age" : 20, "gender" : "M", "course" : "MBA", "marks" : 59 }


SQL’s “AND” operator equivalent in MongoDB?

Syntax

db.students.find([query],[fields])

Example : Lists all the female MBA students

>db.students.find( { $and: [ {course:"MBA"}, {"gender":"F"} ]  } )
{ "_id" : ObjectId("50d09dd394fd058d725d1853"), "name" : "Anjali", "age" : 20, "gender" : "F", "course" : "MBA", "marks" : 67 }

SQL’s “between” operator equivalent in MongoDB?

Example : Students who obtained marks between 70 and 80

>db.students.find({  $and : [ { marks:{$gte:70}}  , { marks:{$lte:80}} ]  } )
{ "_id" : ObjectId("50cebe81984024d0901bad61"), "name" : "John", "age" : 21, "gender" : "M", "course" : "BTECH", "marks" : 79 }

SQL’s “order by” equivalent in MongoDB?

Example : Listing students in the descending order of marks

>db.students.find({},{_id:0,name:1,marks:1} ).sort({marks:-1})
{ "name" : "Firoz", "marks" : 88 }
{ "name" : "Rajesh", "marks" : 81 }
{ "name" : "John", "marks" : 79 }
{ "name" : "Alan", "marks" : 68 }
{ "name" : "Anjali", "marks" : 67 }
{ "name" : "Ali Khan", "marks" : 59 }

Finding total number objects in a collection?

Example : Getting students count

> db.students.find( ).count()
6

Limiting number of objects returned in a query?

Example : Listing top 3 students (based on marks ) in BTECH course

>db.students.find({course:"BTECH"},{_id:0,name:1,marks:1}).
... sort({marks:-1}).
... limit(3)
{ "name" : "Firoz", "marks" : 88 }
{ "name" : "Rajesh", "marks" : 81 }
{ "name" : "John", "marks" : 79 }

How to hire me?

I am George Mathew, working as software architect and Android app developer at wptrafficanalyzer.in

You can hire me on hourly basis or on project basis for Android applications development.

For hiring me, please mail your requirements to info@wptrafficanalyzer.in.

My other blogs
store4js.blogspot.com


Android Knowledge Quiz

Ready to test your knowledge in Android? Take this quiz :



Tags: , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

Be friend at g+

Subscribe for Lastest Updates

FBFPowered by ®Google Feedburner