Contents of page >
1) Query (read/ display/ find/ search/ select) document (record/row) in collection (table) in mongoDB >
We will use find method of mongoDB.
Let's create new collection and insert document in it before finding >
> db.employee.insert({_id : 1, firstName:"ankit"})
> db.employee.insert({_id : 2, firstName:"ankit", salary : 1000 })
> db.employee.insert({_id : 3, firstName:"sam", salary : 2000 })
> db.employee.insert({_id : 4, firstName:"neh", salary : 3000 })
|
First line above will create table (or collection) (if table already exists it will insert documents in it).
FIND Example > Query all documents of collection using find() method>
> db.employee.find()
|
Output>
{ "_id" : 1, "firstName" : "ankit" }
{ "_id" : 2, "firstName" : "ankit", "salary" : 1000 }
{ "_id" : 3, "firstName" : "sam", "salary" : 2000 }
{ "_id" : 4, "firstName" : "neh", "salary" : 3000 }
|
4 documents were found.
2) Display documents of collection in formatted manner.
First, Let's create new collection and insert document in it >
> db.employee.insert({_id : 1, firstName:"ankit"})
> db.employee.insert({_id : 2, firstName:"ankit", salary : 1000 })
> db.employee.insert({_id : 3, firstName:"sam", salary : 2000 })
> db.employee.insert({_id : 4, firstName:"neh", salary : 3000 })
|
First line above will create table (or collection) (if table already exists it will insert documents in it).
FIND Example > Query all document of collection using find().pretty() method>
> db.employee.find().pretty()
|
Output>
{
"_id" : 1,
"firstName" : "ankit"
}
{
"_id" : 2,
"firstName" : "ankit",
"salary" : 1000
}
{
"_id" : 3,
"firstName" : "sam",
"salary" : 2000
}
{
"_id" : 4,
"firstName" : "neh",
"salary" : 3000
}
|
4 documents were found.
3) Selecting specific fields of documents in collection in MongoDB (Projections in MongoDB)
First, Let's create new collection and insert document in it >
> db.employee.insert({_id : 1, firstName:"ankit"})
> db.employee.insert({_id : 2, firstName:"ankit", salary : 1000 })
> db.employee.insert({_id : 3, firstName:"sam", salary : 2000 })
> db.employee.insert({_id : 4, firstName:"neh", salary : 3000 })
|
First line above will create table (or collection) (if table already exists it will insert documents in it).
3.1) FIND Example > Display only firstName field of document of collection using find() method>
> db.employee.find( {}, {firstName : 1})
|
Output>
{ "_id" : 1, "firstName" : "ankit" }
{ "_id" : 2, "firstName" : "ankit" }
{ "_id" : 3, "firstName" : "sam" }
{ "_id" : 4, "firstName" : "neh" }
|
By default _id field is always shown.
3.2) FIND Example > Display only firstName and salary field of document of collection using find() method>
> db.employee.find( {}, {firstName : 1, salary : 1})
|
Output>
{ "_id" : 1, "firstName" : "ankit" }
{ "_id" : 2, "firstName" : "ankit", "salary" : 1000 }
{ "_id" : 3, "firstName" : "sam", "salary" : 2000 }
{ "_id" : 4, "firstName" : "neh", "salary" : 3000 }
|
By default _id field is always shown.
3.4) FIND Example > Display only firstName and salary field of document of collection, But avoid _id field to be displayed using find() method>
> db.employee.find( {}, { _id :0, firstName : 1, salary : 1})
|
Output>
{ "firstName" : "ankit" }
{ "firstName" : "ankit", "salary" : 1000 }
{ "firstName" : "sam", "salary" : 2000 }
{ "firstName" : "neh", "salary" : 3000 }
|
3.5) FIND Example > Display only firstName and salary field of document of collection where salary > 1000, But avoid _id field to be displayed using find() method>
> db.employee.find( { salary : {$gt : 1000} }, { _id :0, firstName : 1, salary : 1})
|
Output>
{ "firstName" : "sam", "salary" : 2000 }
{ "firstName" : "neh", "salary" : 3000 }
|
4) FIND Example > find document where _id=1
First, Let's create new collection and insert document in it >
> db.employee.insert({_id : 1, firstName:"ankit"})
> db.employee.insert({_id : 2, firstName:"ankit", salary : 1000 })
> db.employee.insert({_id : 3, firstName:"sam", salary : 2000 })
> db.employee.insert({_id : 4, firstName:"neh", salary : 3000 })
|
First line above will create table (or collection) (if table already exists it will insert documents in it).
FIND Example > find document where _id=1 >
> db.employee.find({_id:1})
|
Output>
{ "_id" : 1, "firstName" : "ankit" }
|
FIND Example > find document where firstName= "sam" >
> db.employee.find({firstName:"sam"})
|
Output>
{ "_id" : 3, "firstName" : "sam", "salary" : 2000 }
|
5) AND condition - using $and operator on document in collection in MongoDB
First, Let's create new collection and insert document in it >
> db.employee.insert({_id : 1, firstName:"ankit"})
> db.employee.insert({_id : 2, firstName:"ankit", salary : 1000 })
> db.employee.insert({_id : 3, firstName:"sam", salary : 2000 })
> db.employee.insert({_id : 4, firstName:"neh", salary : 3000 })
|
First line above will create table (or collection) (if table already exists it will insert documents in it).
use AND condition - using $and operator.
Find employee where
_id = 1 and firstName = "ankit"
by using find method and $and operator.
> db.employee.find(
{$and : [
{_id:1},
{firstName:"ankit"}
]
}
)
|
Output>
{ "_id" : 1, "firstName" : "ankit" }
|
6) OR condition - using $or operator on document in collection in MongoDB
First, Let's create new collection and insert document in it >
> db.employee.insert({_id : 1, firstName:"ankit"})
> db.employee.insert({_id : 2, firstName:"ankit", salary : 1000 })
> db.employee.insert({_id : 3, firstName:"sam", salary : 2000 })
> db.employee.insert({_id : 4, firstName:"neh", salary : 3000 })
|
First line above will create table (or collection) (if table already exists it will insert documents in it).
Use OR condition - using $or operator.
Find employee where
id = 1 or firstName = "ankit"
by using find method and $or operator.
> db.employee.find(
{$or : [
{_id:1},
{firstName:"ankit"}
]
}
)
|
Output>
{ "_id" : 1, "firstName" : "ankit" }
{ "_id" : 2, "firstName" : "ankit", "salary" : 1000 }
|
7) Method 1 to use AND and OR condition >
- using $and operator and
- using $or operator.
First, Let's create new collection and insert document in it >
> db.employee.insert({_id : 1, firstName:"ankit"})
> db.employee.insert({_id : 2, firstName:"ankit", salary : 1000 })
> db.employee.insert({_id : 3, firstName:"sam", salary : 2000 })
> db.employee.insert({_id : 4, firstName:"neh", salary : 3000 })
|
First line above will create table (or collection) (if table already exists it will insert documents in it).
Find employee where
Salary =1000 and ( id = 1 or firstName = "ankit")
by using find method, $and and $or operators.
> db.employee.find({
$and : [{
salary : 1000
}, {
$or : [{
_id : 1
}, {
firstName : "ankit"
}
]
}
]
})
|
Output>
{ "_id" : 2, "firstName" : "ankit", "salary" : 1000 }
|
Method 2 to use AND and OR condition >
- using $and operator and
- using $or operator.
Find employee where
Salary = 1000 and ( id = 1 or firstName = "ankit")
by using find method, $and and $or operators.
Additionally, use $eq to find document where salary =1000
> db.employee.find({
$and : [{
salary : {$eq : 1000}
}, {
$or : [{
_id : 1
}, {
firstName : "ankit"
}
]
}
]
})
|
Output>
{ "_id" : 2, "firstName" : "ankit", "salary" : 1000 }
|
8) GREATER THAN (>) and GREATER THAN EQUALS (>=) conditions - using $where, $gt and $gte operator in MongoDB
First, Let's create new collection and insert document in it >
> db.employee.insert({_id : 1, firstName:"ankit"})
> db.employee.insert({_id : 2, firstName:"ankit", salary : 1000 })
> db.employee.insert({_id : 3, firstName:"sam", salary : 2000 })
> db.employee.insert({_id : 4, firstName:"neh", salary : 3000 })
|
First line above will create table (or collection) (if table already exists it will insert documents in it).
GREATER THAN (>) and GREATER THAN EQUALS (>=) conditions.
8.1) GREATER THAN (>) - using $gt operator.
Find employee where
Salary > 1000
by using find method and $gt operator.
> db.employee.find( { salary : {$gt : 1000} } )
|
Output>
{ "_id" : 3, "firstName" : "sam", "salary" : 2000 }
{ "_id" : 4, "firstName" : "neh", "salary" : 3000 }
|
8.2) GREATER THAN EQUALS (>=) - using $gte operator.
Find employee where
Salary >= 1000
by using find method and $gt operator.
> db.employee.find( { salary : {$gte : 1000} } )
|
Output>
{ "_id" : 2, "firstName" : "ankit", "salary" : 1000 }
{ "_id" : 3, "firstName" : "sam", "salary" : 2000 }
{ "_id" : 4, "firstName" : "neh", "salary" : 3000 }
|
8.3) GREATER THAN (>) - using $where operator.
Find employee where
Salary > 1000
by using find method and $where operator.
> db.employee.find({ $where:"this.salary > 1000"})
|
Output>
{ "_id" : 3, "firstName" : "sam", "salary" : 2000 }
{ "_id" : 4, "firstName" : "neh", "salary" : 3000 }
|
8.4) GREATER THAN EQUALS (>=) - using $where operator.
Find employee where
Salary >= 1000
by using find method and $where operator.
> db.employee.find({ $where:"this.salary >= 1000"})
|
Output>
{ "_id" : 2, "firstName" : "ankit", "salary" : 1000 }
{ "_id" : 3, "firstName" : "sam", "salary" : 2000 }
{ "_id" : 4, "firstName" : "neh", "salary" : 3000 }
|
9) LESS THAN (<) and LESS THAN EQUALS (<=) - using $where, $lt and $lte operator in MongoDB
First, Let's create new collection and insert document in it >
> db.employee.insert({_id : 1, firstName:"ankit"})
> db.employee.insert({_id : 2, firstName:"ankit", salary : 1000 })
> db.employee.insert({_id : 3, firstName:"sam", salary : 2000 })
> db.employee.insert({_id : 4, firstName:"neh", salary : 3000 })
|
First line above will create table (or collection) (if table already exists it will insert documents in it).
LESS THAN (<) and LESS THAN EQUALS (<=) conditions
9.1) LESS THAN (<) - using $lt operator.
Find employee where
Salary < 2000
by using find method and $lt operator.
> db.employee.find( { salary : {$lt : 2000} } )
|
Output>
{ "_id" : 2, "firstName" : "ankit", "salary" : 1000 }
|
9.2) LESS THAN EQUALS (<=) - using $lte operator.
Find employee where
Salary >= 2000
by using find method and $lt operator.
> db.employee.find( { salary : {$lte : 2000} } )
|
Output>
{ "_id" : 2, "firstName" : "ankit", "salary" : 1000 }
{ "_id" : 3, "firstName" : "sam", "salary" : 2000 }
|
9.3) LESS THAN (<) - using $where operator.
Find employee where
Salary < 2000
by using find method and $where operator.
> db.employee.find({ $where:"this.salary < 2000"})
|
Output>
{ "_id" : 2, "firstName" : "ankit", "salary" : 1000 }
|
9.4) LESS THAN EQUALS (<=) - using $where operator.
Find employee where
Salary >= 2000
by using find method and $where operator.
> db.employee.find({ $where:"this.salary <= 2000"})
|
Output>
{ "_id" : 2, "firstName" : "ankit", "salary" : 1000 }
{ "_id" : 3, "firstName" : "sam", "salary" : 2000 }
|
10) EQUALS (=) and NOT EQUALS (!=) conditions - using $where, $eq and $ne operator in MongoDB
EQUALS (=) and NOT EQUALS (!=) conditions
First, Let's create new collection and insert document in it >
> db.employee.insert({_id : 1, firstName:"ankit"})
> db.employee.insert({_id : 2, firstName:"ankit", salary : 1000 })
> db.employee.insert({_id : 3, firstName:"sam", salary : 2000 })
> db.employee.insert({_id : 4, firstName:"neh", salary : 3000 })
|
First line above will create table (or collection) (if table already exists it will insert documents in it).
10.1) EQUALS (=) - using $eq operator.
Find employee where
Salary = 2000
by using find method and $eq operator.
> db.employee.find( { salary : {$eq : 2000} } )
|
Output>
{ "_id" : 3, "firstName" : "sam", "salary" : 2000 }
|
10.2) NOT EQUALS (!=) - using $ne operator.
Find employee where
Salary != 2000
by using find method and $ne operator.
> db.employee.find( { salary : {$ne : 2000} } )
|
Output>
{ "_id" : 1, "firstName" : "ankit" }
{ "_id" : 2, "firstName" : "ankit", "salary" : 1000 }
{ "_id" : 4, "firstName" : "neh", "salary" : 3000 }
|
10.3) EQUALS (=) - using $where operator.
Find employee where
Salary = 2000
by using find method and $where operator.
> db.employee.find({ $where:"this.salary == 2000"})
|
Output>
{ "_id" : 3, "firstName" : "sam", "salary" : 2000 }
|
10.4) NOT EQUALS (!=) - using $where operator.
Find employee where
Salary != 2000
by using find method and $where operator.
> db.employee.find({ $where:"this.salary != 2000"})
|
Output>
{ "_id" : 1, "firstName" : "ankit" }
{ "_id" : 2, "firstName" : "ankit", "salary" : 1000 }
{ "_id" : 4, "firstName" : "neh", "salary" : 3000 }
|
11) Find document from collection where field(column) EXISTS or not in collection in MongoDB >
First, Let's create new collection and insert document in it >
> db.employee.insert({_id : 1, firstName:"ankit"})
> db.employee.insert({_id : 2, firstName:"ankit", salary : 1000 })
> db.employee.insert({_id : 3, firstName:"sam", salary : 2000 })
> db.employee.insert({_id : 4, firstName:"neh", salary : 3000 })
|
First line above will create table (or collection) (if table already exists it will insert documents in it).
11.1) FIND Example > find document from collection where field(column) salary exists >
Now, let's display document of collection where field(column) salary exists >
> db.employee.find({salary:{$exists : true}})
|
Output>
{ "_id" : 2, "firstName" : "ankit", "salary" : 1000 }
{ "_id" : 3, "firstName" : "sam", "salary" : 2000 }
{ "_id" : 4, "firstName" : "neh", "salary" : 3000 }
|
So, you will see that only newly inserted document which contains column salary was displayed.
11.2) Now, let's display documents of collection where field salary DOESN'T exists >
> db.employee.find({ salary : {$exists : false}})
|
Output>
{ "_id" : 1, "firstName" : "ankit" }
|
So, you will see that newly inserted document which contains column salary was NOT displayed.
12) How to Limit number of documents fetched in MongoDB
Limit number of documents(record/rows) fetched from collection(table) in MongoDB
Limit method limits number of documents displayed.
First, Let's create new collection and insert document in it >
> db.employee.insert({_id : 1, firstName:"ankit"})
> db.employee.insert({_id : 2, firstName:"ankit", salary : 1000 })
> db.employee.insert({_id : 3, firstName:"sam", salary : 2000 })
> db.employee.insert({_id : 4, firstName:"neh", salary : 3000 })
|
First line above will create table (or collection) (if table already exists it will insert documents in it).
12.1) FIND Example > Display only first 2 documents of collection in MongoDB >
We will use find() and limit() method>
> db.employee.find().limit(2)
|
Output>
{ "_id" : 1, "firstName" : "ankit" }
{ "_id" : 2, "firstName" : "ankit", "salary" : 1000 }
|
Display only first 2 documents of collection.
12.2) FIND Example > Skip first document and display rest of documents of collection in MongoDB >
We will use find() and skip() method >
> db.employee.find().skip(1)
|
Output>
{ "_id" : 2, "firstName" : "ankit", "salary" : 1000 }
{ "_id" : 3, "firstName" : "sam", "salary" : 2000 }
{ "_id" : 4, "firstName" : "neh", "salary" : 3000 }
|
Skip 1 document of collection and display rest of documents of collection.
12.3) FIND Example > Display only 2nd and 3rd document of collection in MongoDB>
We will use find(), limit() and skip() method >
> db.employee.find().skip(1).limit(2)
|
Output>
{ "_id" : 2, "firstName" : "ankit", "salary" : 1000 }
{ "_id" : 3, "firstName" : "sam", "salary" : 2000 }
|
Skip 1 document of collection and display 2 documents of collection.
12.4) FIND Example > Display only 2 documents of collection in MongoDB where salary >= 1000 >
> db.employee.find( { salary : {$gte : 1000} }).limit(2)
|
Output>
{ "_id" : 2, "firstName" : "ankit", "salary" : 1000 }
{ "_id" : 3, "firstName" : "sam", "salary" : 2000 }
|
13) Sorting (order by) documents in MongoDB
First, Let's create new collection and insert document in it >
> db.employee.insert({_id : 1, firstName:"ankit"})
> db.employee.insert({_id : 2, firstName:"ankit", salary : 1000 })
> db.employee.insert({_id : 3, firstName:"sam", salary : 2000 })
> db.employee.insert({_id : 4, firstName:"neh", salary : 3000 })
|
First line above will create table (or collection) (if table already exists it will insert documents in it).
13.1) FIND Example > Sort documents of collection on basis of salary in ascending order in MongoDB>
We will use find() and sort() method>
> db.employee.find().sort({salary : 1})
|
Output>
{ "_id" : 1, "firstName" : "ankit" }
{ "_id" : 2, "firstName" : "ankit", "salary" : 1000 }
{ "_id" : 3, "firstName" : "sam", "salary" : 2000 }
{ "_id" : 4, "firstName" : "neh", "salary" : 3000 }
|
13.2) Sql query equivalent to above MongoDB query is >
select * from employee
order by salary;
|
13.3) FIND Example > Sort documents of collection on basis of salary in descending order in MongoDB>
We will use find() and sort() method>
> db.employee.find().sort({salary : -1})
|
Output>
{ "_id" : 4, "firstName" : "neh", "salary" : 3000 }
{ "_id" : 3, "firstName" : "sam", "salary" : 2000 }
{ "_id" : 2, "firstName" : "ankit", "salary" : 1000 }
{ "_id" : 1, "firstName" : "ankit" }
|
13.4) Sql query equivalent to above MongoDB query is >
select * from employee
order by salary desc;
|
13.5) FIND Example > Sort documents of collection on basis of firstName, salary in ascending order in MongoDB>
We will use find() and sort() method>
> db.employee.find().sort({ firstName : 1, salary : 1})
|
Output>
{ "_id" : 1, "firstName" : "ankit" }
{ "_id" : 2, "firstName" : "ankit", "salary" : 1000 }
{ "_id" : 4, "firstName" : "neh", "salary" : 3000 }
{ "_id" : 3, "firstName" : "sam", "salary" : 2000 }
|
13.6) FIND Example > Sort documents of collection on basis of salary in ascending order in MongoDB where salary > 1000 >
We will use find() and sort() method>
> db.employee.find({ salary : {$gt : 1000} }).sort({salary : 1})
|
Output>
{ "_id" : 3, "firstName" : "sam", "salary" : 2000 }
{ "_id" : 4, "firstName" : "neh", "salary" : 3000 }
|
13.7) FIND Example > Sort documents of collection on basis of salary in descending order in MongoDB where salary > 1000 >
We will use find() and sort() method>
> db.employee.find({ salary : {$gt : 1000} }).sort({salary : -1})
|
Output>
{ "_id" : 4, "firstName" : "neh", "salary" : 3000 }
{ "_id" : 3, "firstName" : "sam", "salary" : 2000 }
|
14) find document where firstName length is greater (>) than 3
First, Let's create new collection and insert document in it >
> db.employee.insert({_id : 1, firstName:"ankit"})
> db.employee.insert({_id : 2, firstName:"ankit", salary : 1000 })
> db.employee.insert({_id : 3, firstName:"sam", salary : 2000 })
> db.employee.insert({_id : 4, firstName:"neh", salary : 3000 })
|
First line above will create table (or collection) (if table already exists it will insert documents in it).
Let’s find document where firstName length is greater (>) than 3
> db.employee.find({ $where:"this.firstName.length > 3"})
|
Output>
{ "_id" : 1, "firstName" : "ankit" }
{ "_id" : 2, "firstName" : "ankit", "salary" : "1000" }
|
15) Using like statement (as in sql) in MongoDB
15.1) First let's create records before using like statement (as in sql) in MongoDB
Let's create new collection and insert documents in it before using like statement >
> db.testCollection.insert({name: 'abc'})
> db.testCollection.insert({name: 'bcd'})
> db.testCollection.insert({name: 'def'})
|
First line above will create table (or collection) (if table already exists it will insert documents in it).
Read all documents of collection >
> db.testCollection.find()
{ "_id" : ObjectId("585008261127dea5ece72759"), "name" : "abc" }
{ "_id" : ObjectId("585008261127dea5ece7275a"), "name" : "bcd" }
{ "_id" : ObjectId("585008261127dea5ece7275b"), "name" : "def" }
>
|
15.2) Below find document in mongoDB is similar to like '%b%' in sql >
> db.testCollection.find({name : /b/})
{ "_id" : ObjectId("585008261127dea5ece72759"), "name" : "abc" }
{ "_id" : ObjectId("585008261127dea5ece7275a"), "name" : "bcd" }
>
|
15.3) Below find document in mongoDB is similar to like 'b%' in sql >
> db.testCollection.find({name : /^b/})
{ "_id" : ObjectId("585008261127dea5ece7275a"), "name" : "bcd" }
>
|
15.4) Below find document in mongoDB is similar to like '%f' in sql >
> db.testCollection.find({name : /f$/})
{ "_id" : ObjectId("585008261127dea5ece7275b"), "name" : "def" }
>
|
SUMMARY>
So in this mongoDB tutorial we learned how to Query (read/ display/ find/ search/ select) documents in collection in mongoDB.
Having any doubt? or you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.
RELATED LINKS>