MongoDB is one of the most famous NoSQL databases among developers for storing huge amounts of data in the form of documents. We can easily do CRUD (Create, Read, Update, Delete) operations using different methods in a MongoDB database. In this article, we will particularly focus on print document values in the Mongo shell, i.e., read operation.
MongoDB provides various functions by which we can print document values, some of them are given below:
- find()
- findOne()
- findOneAndDelete()
- findOneAndUpdate()
- findAndModify()
Among the above mentioned functions, except find() and findOne() all of them perform an additional operation, such as findOneAndDelete() will find a document, delete it and then return it. Since we only want to return or print the documents without any additional operations, we have to use the find() and findOne().
In this tutorial, we will see both methods, their syntax and learn how to utilise them to print document value in MongoDB Shell. Additionally, we will also learn about the pretty() function to show the output in an easy-to-read format.
MongoDB find() and findOne() Function
Let’s quickly understand the find() and findOne() functions and see what their syntax looks like.
find() Function
The find() function is used to select multiple documents. By this, we can get values foreach document in a collection.
Syntax:
db.collection.find(query, projection)
Parameters:
- query: It is an optional parameter for filtering the selection, if not provided we get all documents in a collection,
- projection: This is also an optional parameter that specifies whether or not to include a field. To include a field you have to specify 1 and to exclude a field you have to specify 0.
findOne() Function
The findOne() function is used to select a single document and if there are multiple documents then it selects the first one. It can only print one document, which is why it is less used than the find() function.
Syntax:
db.collection.findOne(query, projection)
Parameters:
- query: This is an optional parameter for filtering the selection, if not provided we get the very first document in a collection.
- projection: This is also an optional parameter that specifies whether or not to include a field. To include a field you have to specify 1 and to exclude a field you have to specify 0.
Note: Logically, the find() and findOne() do not return the actual document, they simply return a MongoDB cursor object which is a pointer to the output set of documents. The Mongo shell will automatically iterate over that cursor to print each document to the console.
Print Documents Value in the Mongo Shell
Let us get started with the various examples of printing document value in the Mongo shell.
We have already created a collection “drones” having different content for demonstration. Following are the documents inside “drones”:
{
"_id" : ObjectId("61673f46b34f185eb7b2bf0c"),
"onSale" : false,
"name" : "Nimbari Gryphon Medeta 65",
"price" : 77500,
"weight" : "77 kilograms",
"additionalDetails" : {
"material" : "carbon fiber",
"moreUses" : [
"Precision Agriculture",
"Land Inspection",
"Water Inspection",
"Cinematography"
]
},
"utility" : [
"Recreation",
"Photography",
"Videography",
"Delivery",
"Natural Resource Exploration"
]
}
{
"_id" : ObjectId("61673f46b34f185eb7b2bf0d"),
"onSale" : false,
"name" : "X-Strimmer Eye",
"price" : 23500,
"weight" : "24 kilograms",
"additionalDetails" : {
"material" : "glass fiber",
"moreUses" : [
"Precision Agriculture",
"Cinematography"
]
},
"utility" : [
"Recreation",
"Photography",
"Videography",
"Delivery",
"Natural Resource Exploration"
]
}
{
"_id" : ObjectId("61673f46b34f185eb7b2bf0e"),
"onSale" : false,
"name" : "Khai Balemosh Shefqa TRX",
"price" : 120500,
"weight" : "80 kilograms",
"additionalDetails" : {
"material" : "aluminum",
"moreUses" : [
"Precision Agriculture",
"Land Inspection"
]
},
"utility" : [
"Recreation",
"Photography",
"Videography",
"Delivery",
"Natural Resource Exploration"
]
}
{
"_id" : ObjectId("61673f46b34f185eb7b2bf0f"),
"onSale" : false,
"name" : "Sifinist Croma AX",
"price" : 99500,
"weight" : "97 kilograms",
"additionalDetails" : {
"material" : "lithium",
"moreUses" : [
"Precision Agriculture",
"Land Inspection",
"Water Inspection",
"Videography"
]
},
"utility" : [
"Recreation",
"Photography",
"Videography",
"Delivery",
"Natural Resource Exploration"
]
}
{
"_id" : ObjectId("61673f46b34f185eb7b2bf10"),
"onSale" : false,
"name" : "Drovce Finnifield FR-7",
"price" : 87600,
"weight" : "13 kilograms",
"additionalDetails" : {
"material" : "polysterene",
"moreUses" : [
"Precision Agriculture",
"Land Inspection",
"Water Inspection",
"Videography"
]
},
"utility" : [
"Recreation",
"Photography",
"Videography",
"Delivery",
"Natural Resource Exploration"
]
}
{
"_id" : ObjectId("6195514a50f8bacf51bdb6ac"),
"onSale" : true,
"name" : "PlayGarra 2078-56",
"price" : 15000,
"weight" : "5 kilograms",
"additionalDetails" : {
"material" : "polysterene",
"moreUses" : [
"Cinematography",
"Wildlife Watching"
]
},
"utility" : [
"Recreation",
"Photography",
"Videography",
"Delivery",
"Natural Resource Exploration"
]
}
{
"_id" : ObjectId("6195514a50f8bacf51bdb6ad"),
"onSale" : true,
"name" : "Frinty Gemini 3X70",
"price" : 55000,
"weight" : "22 kilograms",
"additionalDetails" : {
"material" : "aluminum",
"moreUses" : [
"Cinematography",
"Wildlife Watching",
"Precision Agriculture",
"Land Inspection",
"Water Inspection",
"Videography"
]
},
"utility" : [
"Recreation",
"Photography",
"Videography",
"Delivery",
"Natural Resource Exploration"
]
}
{
"_id" : ObjectId("6195514a50f8bacf51bdb6ae"),
"onSale" : true,
"name" : "Rilche Gabbana Flier RG950",
"price" : 110000,
"weight" : "100 kilograms",
"additionalDetails" : {
"material" : "lithium",
"moreUses" : [
"Cinematography",
"Wildlife Watching",
"Precision Agriculture",
"Land Inspection",
"Water Inspection",
"Videography"
]
},
"utility" : [
"Recreation",
"Photography",
"Videography",
"Delivery",
"Natural Resource Exploration"
]
}
Using the find() Function to Print Document Value in the Mongo Shell
In this example, we will be using the find() to print document values in the shell.
Disclaimer: The result printed below looks messy.
> db.drones.find()
{ "_id" : ObjectId("61673f46b34f185eb7b2bf0c"), "onSale" : false, "name" : "Nimbari Gryphon Medeta 65", "price" : 77500, "weight" : "77 kilograms", "additionalDetails" : { "material" : "carbon fiber", "moreUses" : [ "Precision Agriculture", "Land Inspection", "Water Inspection", "Cinematography" ] }, "utility" : [ "Recreation", "Photography", "Videography", "Delivery", "Natural Resource Exploration" ] }
{ "_id" : ObjectId("61673f46b34f185eb7b2bf0d"), "onSale" : false, "name" : "X-Strimmer Eye", "price" : 23500, "weight" : "24 kilograms", "additionalDetails" : { "material" : "glass fiber", "moreUses" : [ "Precision Agriculture", "Cinematography" ] }, "utility" : [ "Recreation", "Photography", "Videography", "Delivery", "Natural Resource Exploration" ] }
{ "_id" : ObjectId("61673f46b34f185eb7b2bf0e"), "onSale" : false, "name" : "Khai Balemosh Shefqa TRX", "price" : 120500, "weight" : "80 kilograms", "additionalDetails" : { "material" : "aluminum", "moreUses" : [ "Precision Agriculture", "Land Inspection" ] }, "utility" : [ "Recreation", "Photography", "Videography", "Delivery", "Natural Resource Exploration" ] }
{ "_id" : ObjectId("61673f46b34f185eb7b2bf0f"), "onSale" : false, "name" : "Sifinist Croma AX", "price" : 99500, "weight" : "97 kilograms", "additionalDetails" : { "material" : "lithium", "moreUses" : [ "Precision Agriculture", "Land Inspection", "Water Inspection", "Videography" ] }, "utility" : [ "Recreation", "Photography", "Videography", "Delivery", "Natural Resource Exploration" ] }
{ "_id" : ObjectId("61673f46b34f185eb7b2bf10"), "onSale" : false, "name" : "Drovce Finnifield FR-7", "price" : 87600, "weight" : "13 kilograms", "additionalDetails" : { "material" : "polysterene", "moreUses" : [ "Precision Agriculture", "Land Inspection", "Water Inspection", "Videography" ] }, "utility" : [ "Recreation", "Photography", "Videography", "Delivery", "Natural Resource Exploration" ] }
{ "_id" : ObjectId("6195514a50f8bacf51bdb6ac"), "onSale" : true, "name" : "PlayGarra 2078-56", "price" : 15000, "weight" : "5 kilograms", "additionalDetails" : { "material" : "polysterene", "moreUses" : [ "Cinematography", "Wildlife Watching" ] }, "utility" : [ "Recreation", "Photography", "Videography", "Delivery", "Natural Resource Exploration" ] }
{ "_id" : ObjectId("6195514a50f8bacf51bdb6ad"), "onSale" : true, "name" : "Frinty Gemini 3X70", "price" : 55000, "weight" : "22 kilograms", "additionalDetails" : { "material" : "aluminum", "moreUses" : [ "Cinematography", "Wildlife Watching", "Precision Agriculture", "Land Inspection", "Water Inspection", "Videography" ] }, "utility" : [ "Recreation", "Photography", "Videography", "Delivery", "Natural Resource Exploration" ] }
{ "_id" : ObjectId("6195514a50f8bacf51bdb6ae"), "onSale" : true, "name" : "Rilche Gabbana Flier RG950", "price" : 110000, "weight" : "100 kilograms", "additionalDetails" : { "material" : "lithium", "moreUses" : [ "Cinematography", "Wildlife Watching", "Precision Agriculture", "Land Inspection", "Water Inspection", "Videography" ] }, "utility" : [ "Recreation", "Photography", "Videography", "Delivery", "Natural Resource Exploration" ] }
Pretty Printing Documents in the Mongo Shell
The pretty() function in MongoDB can format the query results with a proper indentation and produce readable output that looks like JSON which makes it easier to view and understand the data.
Execute the below command to display documents with proper formatting:
> db.drones.find().pretty()
{
"_id" : ObjectId("61673f46b34f185eb7b2bf0c"),
"onSale" : false,
"name" : "Nimbari Gryphon Medeta 65",
"price" : 77500,
"weight" : "77 kilograms",
"additionalDetails" : {
"material" : "carbon fiber",
"moreUses" : [
"Precision Agriculture",
"Land Inspection",
"Water Inspection",
"Cinematography"
]
},
"utility" : [
"Recreation",
"Photography",
"Videography",
"Delivery",
"Natural Resource Exploration"
]
}
{
"_id" : ObjectId("61673f46b34f185eb7b2bf0d"),
"onSale" : false,
"name" : "X-Strimmer Eye",
"price" : 23500,
"weight" : "24 kilograms",
"additionalDetails" : {
"material" : "glass fiber",
"moreUses" : [
"Precision Agriculture",
"Cinematography"
]
},
"utility" : [
"Recreation",
"Photography",
"Videography",
"Delivery",
"Natural Resource Exploration"
]
}
{
"_id" : ObjectId("61673f46b34f185eb7b2bf0e"),
"onSale" : false,
"name" : "Khai Balemosh Shefqa TRX",
"price" : 120500,
"weight" : "80 kilograms",
"additionalDetails" : {
"material" : "aluminum",
"moreUses" : [
"Precision Agriculture",
"Land Inspection"
]
},
"utility" : [
"Recreation",
"Photography",
"Videography",
"Delivery",
"Natural Resource Exploration"
]
}
{
"_id" : ObjectId("61673f46b34f185eb7b2bf0f"),
"onSale" : false,
"name" : "Sifinist Croma AX",
"price" : 99500,
"weight" : "97 kilograms",
"additionalDetails" : {
"material" : "lithium",
"moreUses" : [
"Precision Agriculture",
"Land Inspection",
"Water Inspection",
"Videography"
]
},
"utility" : [
"Recreation",
"Photography",
"Videography",
"Delivery",
"Natural Resource Exploration"
]
}
{
"_id" : ObjectId("61673f46b34f185eb7b2bf10"),
"onSale" : false,
"name" : "Drovce Finnifield FR-7",
"price" : 87600,
"weight" : "13 kilograms",
"additionalDetails" : {
"material" : "polysterene",
"moreUses" : [
"Precision Agriculture",
"Land Inspection",
"Water Inspection",
"Videography"
]
},
"utility" : [
"Recreation",
"Photography",
"Videography",
"Delivery",
"Natural Resource Exploration"
]
}
{
"_id" : ObjectId("6195514a50f8bacf51bdb6ac"),
"onSale" : true,
"name" : "PlayGarra 2078-56",
"price" : 15000,
"weight" : "5 kilograms",
"additionalDetails" : {
"material" : "polysterene",
"moreUses" : [
"Cinematography",
"Wildlife Watching"
]
},
"utility" : [
"Recreation",
"Photography",
"Videography",
"Delivery",
"Natural Resource Exploration"
]
}
{
"_id" : ObjectId("6195514a50f8bacf51bdb6ad"),
"onSale" : true,
"name" : "Frinty Gemini 3X70",
"price" : 55000,
"weight" : "22 kilograms",
"additionalDetails" : {
"material" : "aluminum",
"moreUses" : [
"Cinematography",
"Wildlife Watching",
"Precision Agriculture",
"Land Inspection",
"Water Inspection",
"Videography"
]
},
"utility" : [
"Recreation",
"Photography",
"Videography",
"Delivery",
"Natural Resource Exploration"
]
}
{
"_id" : ObjectId("6195514a50f8bacf51bdb6ae"),
"onSale" : true,
"name" : "Rilche Gabbana Flier RG950",
"price" : 110000,
"weight" : "100 kilograms",
"additionalDetails" : {
"material" : "lithium",
"moreUses" : [
"Cinematography",
"Wildlife Watching",
"Precision Agriculture",
"Land Inspection",
"Water Inspection",
"Videography"
]
},
"utility" : [
"Recreation",
"Photography",
"Videography",
"Delivery",
"Natural Resource Exploration"
]
}
Using the findOne() Function to Print Document Value in the Mongo Shell
In this example, we will be using the findOne() to print document values in the shell.
> db.drones.findOne(
... { name: 'Rilche Gabbana Flier RG950' }
... )
{
"_id" : ObjectId("6195514a50f8bacf51bdb6ae"),
"onSale" : true,
"name" : "Rilche Gabbana Flier RG950",
"price" : 110000,
"weight" : "100 kilograms",
"additionalDetails" : {
"material" : "lithium",
"moreUses" : [
"Cinematography",
"Wildlife Watching",
"Precision Agriculture",
"Land Inspection",
"Water Inspection",
"Videography"
]
},
"utility" : [
"Recreation",
"Photography",
"Videography",
"Delivery",
"Natural Resource Exploration"
]
}
Also Read:
- Top 5 Best Programming Languages to Learn in 2023
- How to run Node.js files in VS Code
- Node.js vs Java
Conclusion
In this article, we have seen two functions for printing MongoDB document values without the need to create new collections or documents. The first is find() which can print multiple documents or all the documents from a collection. The second is findOne() which can print only a single document. MongoDB developers and the community recommend using the find() as it can select and print all records at once. At last, we hope this post helped you find the answers to your questions.
References
- https://docs.mongodb.com/v4.2/reference/method/db.collection.find/
- https://docs.mongodb.com/manual/reference/method/db.collection.findOne/
- Pretty print in MongoDB shell as default – Stack Overflow