MongoDB শেলে কোয়েরি রেজাল্ট সুন্দর ও পঠনীয় ফরম্যাটে দেখতে findOne() এবং toArray() ব্যবহার করা হয়।
নমুনা ডেটা তৈরি করা
betterFormatDemo কালেকশনে তিনটি ডকুমেন্ট ইনসার্ট করি।
> 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();
আউটপুট: এক লাইনে সিকुड़ে থাকা ফরম্যাটে দেখায়, পড়তে কঠিন।
Case 1: findOne() ব্যবহার করা
শুধু প্রথম ডকুমেন্ট সুন্দর ফরম্যাটে দেখাতে:
> db.betterFormatDemo.findOne();
আউটপুট:
{
"_id" : ObjectId("5cd7ab826d78f205348bc640"),
"StudentName" : "Adam Smith",
"StudentScores" : [ 98, 67, 89 ]
}
Case 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 ] }
]
টিপস: pretty()ও ব্যবহার করা যায়: db.coll.find().pretty()। toArray() প্রোগ্রামেটিক্যালি অ্যারে আকারে ডেটা পাওয়ার জন্য উপযোগী।