The tutorial provides the different methods available to Query document in MongoDB collection. A document can be read or queried in mongodb using find() , findOne(), pretty() methods which we will discuss in detail with examples.
Query document methods in MongoDB
Method Name – find() :
Syntax: > db.collectionName.find()
The find() method is the basic method to query the document in a collection. This method lists down all the documents available in a collection.
Insert the document in the Collection – oracleappsUsers
> db.oracelappsUsers.insert({ … name: "Rohit Verma", … userType: "Subscriber", … description: "MongoDB insert document Tutorial", … createdBy: "oracelappshelp.com" … }) WriteResult({ "nInserted" : 1 })
Query the document with find () in the collection – oracleappsUsers. It will list down all the available documents.
> db.oracelappsUsers.find() { "_id" : ObjectId("5f12fd48d4a587e31cddd7e5"), "name" : "Mohit Sharma", "userType" : "Admin", "description" : "MongoDB insert document sample", "createdBy" : "oracelappshelp.com" } { "_id" : ObjectId("5f130f1c3a96070d59afd2c0"), "name" : "Rohit Verma", "userType" : "Subscriber", "description" : "MongoDB insert document Tutorial", "createdBy" : "oracelappshelp.com" }
Method Name – pretty
Syntax – >db.collectionName.find().pretty()
The pretty() method is allowed to format the Query document result. The below given is the example for find().pretty()
> db.oracelappsUsers.find().pretty() { "_id" : ObjectId("5f12fd48d4a587e31cddd7e5"), "name" : "Mohit Sharma", "userType" : "Admin", "description" : "MongoDB insert document sample", "createdBy" : "oracelappshelp.com" } { "_id" : ObjectId("5f130f1c3a96070d59afd2c0"), "name" : "Rohit Verma", "userType" : "Subscriber", "description" : "MongoDB insert document Tutorial", "createdBy" : "oracelappshelp.com" }
Method Name – findOne()
Syntax – >db.collectionName.findOne(<search_attribute>)
The findOne() method in MongoDB allows to query a single document by providing the document attribute details. The below given example depicts the usage of findOne() to query document in a collection.
> db.oracelappsUsers.findOne({name: "Mohit Sharma"}) { "_id" : ObjectId("5f12fd48d4a587e31cddd7e5"), "name" : "Mohit Sharma", "userType" : "Admin", "description" : "MongoDB insert document sample", "createdBy" : "oracelappshelp.com" }
Query Operators in MongoDB
RDBMS provides various operators to retrieve the data based on specified conditions like Where , <= , >= , AND , OR
MongoDB provides the RDBMS equivalent operators to query the document in a collection.
S. No | RDBMS Search Data Query | MongoDB Query Document |
1 | SELECT * from Orders | db.orders.find( {} ) |
2 | SELECT * FROM orders WHERE status = “COMPLETED” | db.orders.find( { status: “COMPLETED” } ) |
3 | SELECT * FROM orders WHERE status in (“INPROGRESS”, “PENDING”) | db.orders.find( { status: { $in: [ “INPROGRESS”, “PENDING” ] } } ) |
4 | SELECT * FROM orders WHERE status = “PENDING” AND price < 2000 | db.orders.find( { status: “PENDING”, price: { $lt: 2000 } } ) |
5 | SELECT * FROM orders WHERE status = “PENDING” OR price < 2000 | db.orders.find( { $or: [ { status: “PENDING” }, { price: { $lt: 2000 } } ] } ) |
6 | SELECT * FROM orders WHERE status = “PENDING” AND ( price < 3000 OR item LIKE “Laptop%”) | db.orders.find( { status: “PENDING”, $or: [ { price: { $lt: 3000 } }, { item: /^Laptop/ } ] } ) |