Read Data

To read data in MongoDB Database, we have to use find method.

Find Method is used to read data in MongoDB. We can use find method to fetch single or multiple data in MongoDB. There are other filters, operators and methods of find method.

We will also learn how to getCollections and to get distinct values of same name.

Some methods like, limit, skip, sort and count are used with find() to get sorted data, limit, skip and to count.

Here is the sample database used in our queries.

Sample Database


[{"name": "swift","type": "hatchback","price":800000},
 {"name": "ciaz","type": "sedan","price":1000000},
 {"name": "baleno","type": "hatchback","price":850000},
 {"name": "scross","type": "crossover","price":11500000},
 {"name": "brezza","type": "suv","price":990000},
 {"name": "gypsy","type": "suv","price":750000}
]    

show Collection

show collections or db.getCollectionNames method will return name of all collections stored in database.

show collections
db.getCollectionNames()

Find

Find Method is used to get all documents or document in MongoDB Collection. Its same like SELECT * from table in SQL. We cal also use And, or, not operators in find.

{ "_id" : ObjectId("5eb44282ea2adece905a24b0"), "name" : "swift", "type" : "hatchback", "price":800000 }
{ "_id" : ObjectId("5eb5378791fc73f290f38251"), "name" : "ciaz", "type" : "sedan", "price":1000000 }
{ "_id" : ObjectId("5eb5378791fc73f290f38252"), "name" : "baleno", "type" : "hatchback", "price":850000 }
{ "_id" : ObjectId("5eb53a9191fc73f290f38253"), "name" : "scross", "type" : "crossover", "price":11500000 }
{ "_id" : ObjectId("5eb53b3491fc73f290f38254"), "name" : "brezza", "type" : "suv", "price":990000 }
{ "_id" : ObjectId("5eb53b3491fc73f290f38255"), "name" : "gypsy", "type" : "suv", "price":750000 }    
db.suzuki.find()

Pretty

To print data data in pretty JSON, use pretty() method of find().

{
        "_id" : ObjectId("5eb81a2fbe672314a543269e"),
        "name" : "swift",
        "type" : "hatchback",
        "price" : 800000
}
{
        "_id" : ObjectId("5eb81a2fbe672314a543269f"),
        "name" : "ciaz",
        "type" : "sedan",
        "price" : 1000000
}
{
        "_id" : ObjectId("5eb81a2fbe672314a54326a0"),
        "name" : "baleno",
        "type" : "hatchback",
        "price" : 850000
}
{
        "_id" : ObjectId("5eb81a2fbe672314a54326a1"),
        "name" : "scross",
        "type" : "crossover",
        "price" : 11500000
}
{
        "_id" : ObjectId("5eb81a2fbe672314a54326a2"),
        "name" : "brezza",
        "type" : "suv",
        "price" : 990000
}
{
        "_id" : ObjectId("5eb81a2fbe672314a54326a3"),
        "name" : "gypsy",
        "type" : "suv",
        "price" : 750000
}
   
db.suzuki.find().pretty()

Sort()

To get sorted data in Mongodb, use sort method of find method with argument object. See example

Alphabetical Sort

{ "_id" : ObjectId("5eb81cd8b4957dcc9bc5ceef"), "name" : "alto", "type" : "hatchback", "price" : 350000 }
{ "_id" : ObjectId("5eb81a2fbe672314a54326a0"), "name" : "baleno", "type" : "hatchback", "price" : 850000 }
{ "_id" : ObjectId("5ebbf05dbcbb54669293d48d"), "name" : "baleno rs", "type" : "hatchback", "price" : 950000 }
{ "_id" : ObjectId("5eb81a2fbe672314a54326a2"), "name" : "brezza", "type" : "suv", "price" : 990000 }
{ "_id" : ObjectId("5eb81a2fbe672314a543269f"), "name" : "ciaz", "type" : "sedan", "price" : 1000000 }
{ "_id" : ObjectId("5eb81a2fbe672314a54326a3"), "name" : "gypsy", "type" : "suv", "price" : 750000 }   
db.suzuki.find().sort({'name':1})

Reverse Sort

{ "_id" : ObjectId("5eb81a2fbe672314a543269e"), "name" : "swift", "type" : "hatchback", "price" : 800000 }
{ "_id" : ObjectId("5eb81a2fbe672314a54326a1"), "name" : "scross", "type" : "crossover", "price" : 11500000 }
{ "_id" : ObjectId("5eb81a2fbe672314a54326a3"), "name" : "gypsy", "type" : "suv", "price" : 750000 }
{ "_id" : ObjectId("5eb81a2fbe672314a543269f"), "name" : "ciaz", "type" : "sedan", "price" : 1000000 }
{ "_id" : ObjectId("5eb81a2fbe672314a54326a2"), "name" : "brezza", "type" : "suv", "price" : 990000 }
{ "_id" : ObjectId("5eb81a2fbe672314a54326a0"), "name" : "baleno", "type" : "hatchback", "price" : 850000 }
db.suzuki.find().sort({'name':-1})

Count

Count Method is used to count the results. We can also length method to count.

6
db.suzuki.find().count()
2
db.suzuki.count( { type : 'suv' } )
6
db.suzuki.find().length()

Skip

skip method is used to skip documents.

{ "_id" : ObjectId("5eb5378791fc73f290f38252"), "name" : "baleno", "type" : "hatchback", "price":850000 }
{ "_id" : ObjectId("5eb53a9191fc73f290f38253"), "name" : "scross", "type" : "crossover", "price":11500000 }
{ "_id" : ObjectId("5eb53b3491fc73f290f38254"), "name" : "brezza", "type" : "suv", "price":990000 }
{ "_id" : ObjectId("5eb53b3491fc73f290f38255"), "name" : "gypsy", "type" : "suv", "price":750000 }
db.suzuki.find().skip(2)

limit

limit method of find is used to limit data from collections. The argument of limit is a number. To get first document from collection, use limit(1). Similarly to get two documents, use limit(2). See example

{ "_id" : ObjectId("5eb44282ea2adece905a24b0"), "name" : "swift", "type" : "hatchback", "price":800000 } 
db.suzuki.find().limit(1)
{ "_id" : ObjectId("5eb44282ea2adece905a24b0"), "name" : "swift", "type" : "hatchback", "price":800000 } 
{ "_id" : ObjectId("5eb5378791fc73f290f38251"), "name" : "ciaz", "type" : "sedan", "price":1000000 }
db.suzuki.find().limit(2)

getCollectionNames

db.getCollectionNames method is used to get all collection names in MongoDB.

[ "honda", "hyundai", "maruti", "stores", "suzuki" ]
db.getCollectionNames()

distinct

distinct method is used to get all values in array by using name of field.


[
    "baleno",
    "brezza",
    "ciaz",
    "gypsy",
    "scross",
    "swift"
]    

db.suzuki.distinct('name')

[ "crossover", "hatchback", "sedan", "suv" ]    

db.suzuki.distinct('type')

Specify Equality Condition using field:value

Specify Equality Condition can be used by using { field:value } expression in find method.

{ "_id" : ObjectId("5eb44282ea2adece905a24b0"), "name" : "swift", "type" : "hatchback", "price":800000 } 
db.suzuki.find({name:"swift"})
{ "_id" : ObjectId("5eb44282ea2adece905a24b0"), "name" : "swift", "type" : "hatchback", "price":800000 } 
{ "_id" : ObjectId("5eb5378791fc73f290f38252"), "name" : "baleno", "type" : "hatchback", "price":850000 }
db.suzuki.find({type:"hatchback"})

Using $in

{ "_id" : ObjectId("5eb44282ea2adece905a24b0"), "name" : "swift", "type" : "hatchback", "price" : 800000 }
{ "_id" : ObjectId("5eb5378791fc73f290f38251"), "name" : "ciaz", "type" : "sedan", "price" : 1000000 }
{ "_id" : ObjectId("5eb5378791fc73f290f38252"), "name" : "baleno", "type" : "hatchback", "price" : 8500000 }
db.suzuki.find({type:{$in:['hatchback','sedan']}})

Using AND

{ "_id" : ObjectId("5eb53b3491fc73f290f38255"), "name" : "gypsy", "type" : "suv", "price" : 750000 }
db.suzuki.find({type:'suv',price:750000})

Using $lt

{ "_id" : ObjectId("5eb44282ea2adece905a24b0"), "name" : "swift", "type" : "hatchback", "price" : 800000 }
{ "_id" : ObjectId("5eb5378791fc73f290f38252"), "name" : "baleno", "type" : "hatchback", "price" : 8500000 }
db.suzuki.find({type:'hatchback',price:{$lt:700000}})

Using $gt

{ "_id" : ObjectId("5eb44282ea2adece905a24b0"), "name" : "swift", "type" : "hatchback", "price" : 800000 }
{ "_id" : ObjectId("5eb5378791fc73f290f38252"), "name" : "baleno", "type" : "hatchback", "price" : 8500000 }
db.suzuki.find({type:'suv',price:{$gt:700000}})

Using OR

{ "_id" : ObjectId("5eb5378791fc73f290f38252"), "name" : "baleno", "type" : "hatchback", "price" : 8500000 }
{ "_id" : ObjectId("5eb53a9191fc73f290f38253"), "name" : "scross", "type" : "crossover", "price" : 11500000 }
db.suzuki.find({ $or: [{price:{$gt:1000000}},{type:'crossover'} ] })

Find One

findOne method is used to fine one data from collection.

{
        "_id" : ObjectId("5eb44282ea2adece905a24b0"),
        "name" : "swift",
        "type" : "hatchback",
        "price" : 800000
}
db.suzuki.findOne()