In this tutorial, I will explain how to print document values in the mongo shell.
We will print document values in the mongo shell by using the find() function in MongoDB. the find operation comes with various other functions:
- find()
- findOne()
- findOneAndDelete()
- findOneAndUpdate()
- findAndModify()
I will cover the first 2 functions on the list in this guide. That is, find() and findOne() functions to print document value in the mongo shell.
This guide will provide examples for each of the functions to be discussed. Moreover, I will also include different types of querying examples using the find operation in the mongo shell. So, let us get this guide started!
The Find Operations Syntax
Let us take a quick look at the syntaxes of find operations that we are going to discuss in this tutorial today.
find() function:
db.collection.find(query, projection)
findOne() function:
db.collection.findOne(query, projection)
Print Documents Value in the Mongo Shell
Let us get started with the various examples to print document value in the mongo shell.
This is the collection we will use for this guide:
{
"_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, I will use the find() function to print document value in the mongo shell.
Disclaimer: the result printed below looks very 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 the mongo shell makes room for a readable output that comes with proper indentation and clarity.
> 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, I will use the findOne() function to print document values in the mongo 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"
]
}
Conclusion
Learn to print document value in the mongo shell with the above examples in MongoDB.
References
https://docs.mongodb.com/v4.2/reference/method/db.collection.find/
https://docs.mongodb.com/manual/reference/method/db.collection.findOne/