MongoDB शेल में बेहतर आउटपुट फॉर्मेटिंग
MongoDB शेल डिफ़ॉल्ट रूप से कंप्रेस्ड फॉर्मेट में परिणाम प्रदर्शित करता है। पठनीयता में सुधार के लिए, findOne(), pretty(), या toArray() का उपयोग करें।
नमूना डेटा बनाना
> db.betterFormatDemo.insertOne({"StudentName":"Adam Smith","StudentScores":[98,67,89]});
{ "acknowledged" : true, "insertedId" : ObjectId("5cd7ab826d78f205348bc640") }
> db.betterFormatDemo.insertOne({"StudentName":"John Doe","StudentScores":[67,89,56]});
{ "acknowledged" : true, "insertedId" : ObjectId("5cd7ab936d78f205348bc641") }
> db.betterFormatDemo.insertOne({"StudentName":"Sam Williams","StudentScores":[45,43,33]});
{ "acknowledged" : true, "insertedId" : ObjectId("5cd7aba76d78f205348bc642") }
डिफ़ॉल्ट find() आउटपुट (कंप्रेस्ड)
> db.betterFormatDemo.find();
आउटपुट:
{ "_id" : ObjectId("5cd7ab826d78f205348bc640"), "StudentName" : "Adam Smith", "StudentScores" : [ 98, 67, 89 ] }
{ "_id" : ObjectId("5cd7ab936d78f205348bc641"), "StudentName" : "John Doe", "StudentScores" : [ 67, 89, 56 ] }
{ "_id" : ObjectId("5cd7aba76d78f205348bc642"), "StudentName" : "Sam Williams", "StudentScores" : [ 45, 43, 33 ] }
केस 1: findOne() का उपयोग करना (पहला डॉक्यूमेंट, फॉर्मेटेड)
> db.betterFormatDemo.findOne();
आउटपुट:
{
"_id" : ObjectId("5cd7ab826d78f205348bc640"),
"StudentName" : "Adam Smith",
"StudentScores" : [ 98, 67, 89 ]
}
केस 2: find().toArray() का उपयोग करना (सभी डॉक्यूमेंट्स, फॉर्मेटेड ऐरे)
> db.betterFormatDemo.find().toArray();
आउटपुट:
[
{
"_id" : ObjectId("5cd7ab826d78f205348bc640"),
"StudentName" : "Adam Smith",
"StudentScores" : [ 98, 67, 89 ]
},
{
"_id" : ObjectId("5cd7ab936d78f205348bc641"),
"StudentName" : "John Doe",
"StudentScores" : [ 67, 89, 56 ]
},
{
"_id" : ObjectId("5cd7aba76d78f205348bc642"),
"StudentName" : "Sam Williams",
"StudentScores" : [ 45, 43, 33 ]
}
]
तुलना
| मेथड | रिटर्न्स | फॉर्मेटिंग | उपयोग करें जब |
|---|---|---|---|
find() | कर्सर | कंप्रेस्ड (शेल डिफ़ॉल्ट) | बड़े परिणाम सेट, पुनरावृति |
find().pretty() | कर्सर | सुंदर-प्रिंटेड (प्रति डॉक्यूमेंट मल्टी-लाइन) | शेल में मैन्युअल निरीक्षण |
findOne() | एकल डॉक्यूमेंट | सुंदर-प्रिंटेड | त्वरित जांच, पहला मिलान |
find().toArray() | डॉक्यूमेंट्स का ऐरे | सुंदर-प्रिंटेड ऐरे | छोटे परिणाम सेट, JSON एक्सपोर्ट |
आधुनिक MongoDB शेल (mongosh)
नया mongosh शेल डिफ़ॉल्ट रूप से सुंदर आउटपुट देता है और सिंटैक्स हाइलाइटिंग, ऑटोकम्प्लीशन, और बेहतर फॉर्मेटिंग का समर्थन करता है।