Read Data in MongoDB
Written By: Avinash Malhotra
Updated on
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.
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 }
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 }
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 }
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 }
Count
Count Method is used to count the results. We can also length method to count.
6
2
6
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 }
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 }
{ "_id" : ObjectId("5eb44282ea2adece905a24b0"), "name" : "swift", "type" : "hatchback", "price":800000 } { "_id" : ObjectId("5eb5378791fc73f290f38251"), "name" : "ciaz", "type" : "sedan", "price":1000000 }
getCollectionNames
db.getCollectionNames method is used to get all collection names in MongoDB.
[ "honda", "hyundai", "maruti", "stores", "suzuki" ]
distinct
distinct method is used to get all values in array by using name of field.
[
"baleno",
"brezza",
"ciaz",
"gypsy",
"scross",
"swift"
]
[ "crossover", "hatchback", "sedan", "suv" ]
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 }
{ "_id" : ObjectId("5eb44282ea2adece905a24b0"), "name" : "swift", "type" : "hatchback", "price":800000 } { "_id" : ObjectId("5eb5378791fc73f290f38252"), "name" : "baleno", "type" : "hatchback", "price":850000 }
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 }
Using AND
{ "_id" : ObjectId("5eb53b3491fc73f290f38255"), "name" : "gypsy", "type" : "suv", "price" : 750000 }
Using $lt
{ "_id" : ObjectId("5eb44282ea2adece905a24b0"), "name" : "swift", "type" : "hatchback", "price" : 800000 } { "_id" : ObjectId("5eb5378791fc73f290f38252"), "name" : "baleno", "type" : "hatchback", "price" : 8500000 }
Using $gt
{ "_id" : ObjectId("5eb44282ea2adece905a24b0"), "name" : "swift", "type" : "hatchback", "price" : 800000 } { "_id" : ObjectId("5eb5378791fc73f290f38252"), "name" : "baleno", "type" : "hatchback", "price" : 8500000 }
Using OR
{ "_id" : ObjectId("5eb5378791fc73f290f38252"), "name" : "baleno", "type" : "hatchback", "price" : 8500000 } { "_id" : ObjectId("5eb53a9191fc73f290f38253"), "name" : "scross", "type" : "crossover", "price" : 11500000 }
Find One
findOne method is used to fine one data from collection.
{ "_id" : ObjectId("5eb44282ea2adece905a24b0"), "name" : "swift", "type" : "hatchback", "price" : 800000 }