इसके लिए प्रिंटजसन का इस्तेमाल करें। आइए पहले दस्तावेजों के साथ एक संग्रह बनाएं -
> db.cursorDemo.insertOne({"StudentFullName":"John Smith","StudentAge":23});
{
"acknowledged" : true,
"insertedId" : ObjectId("5cc7f0d08f9e6ff3eb0ce442")
}
> db.cursorDemo.insertOne({"StudentFullName":"John Doe","StudentAge":21});
{
"acknowledged" : true,
"insertedId" : ObjectId("5cc7f0df8f9e6ff3eb0ce443")
}
> db.cursorDemo.insertOne({"StudentFullName":"Carol Taylor","StudentAge":20});
{
"acknowledged" : true,
"insertedId" : ObjectId("5cc7f0eb8f9e6ff3eb0ce444")
}
> db.cursorDemo.insertOne({"StudentFullName":"Chris Brown","StudentAge":24});
{
"acknowledged" : true,
"insertedId" : ObjectId("5cc7f0f88f9e6ff3eb0ce445")
} खोज () विधि की मदद से संग्रह से सभी दस्तावेजों को प्रदर्शित करने के लिए क्वेरी निम्नलिखित है -
> db.cursorDemo.find().pretty();
यह निम्नलिखित आउटपुट उत्पन्न करेगा -
{
"_id" : ObjectId("5cc7f0d08f9e6ff3eb0ce442"),
"StudentFullName" : "John Smith",
"StudentAge" : 23
}
{
"_id" : ObjectId("5cc7f0df8f9e6ff3eb0ce443"),
"StudentFullName" : "John Doe",
"StudentAge" : 21
}
{
"_id" : ObjectId("5cc7f0eb8f9e6ff3eb0ce444"),
"StudentFullName" : "Carol Taylor",
"StudentAge" : 20
}
{
"_id" : ObjectId("5cc7f0f88f9e6ff3eb0ce445"),
"StudentFullName" : "Chris Brown",
"StudentAge" : 24
} Printjson के साथ दस्तावेज़ को पुनरावृति और प्रिंट करने के लिए क्वेरी निम्नलिखित है -
> db.cursorDemo.find().forEach(printjson);
यह निम्नलिखित आउटपुट उत्पन्न करेगा -
{
"_id" : ObjectId("5cc7f0d08f9e6ff3eb0ce442"),
"StudentFullName" : "John Smith",
"StudentAge" : 23
}
{
"_id" : ObjectId("5cc7f0df8f9e6ff3eb0ce443"),
"StudentFullName" : "John Doe",
"StudentAge" : 21
}
{
"_id" : ObjectId("5cc7f0eb8f9e6ff3eb0ce444"),
"StudentFullName" : "Carol Taylor",
"StudentAge" : 20
}
{
"_id" : ObjectId("5cc7f0f88f9e6ff3eb0ce445"),
"StudentFullName" : "Chris Brown",
"StudentAge" : 24
} यदि हम केवल "StudentFullName" और "StudentAge" फ़ील्ड जैसे विशिष्ट फ़ील्ड चाहते हैं, तो दूसरी क्वेरी निम्नलिखित है -
> db.cursorDemo.find({}, { "StudentFullName": 1,"StudentAge":1, "_id": 0 }).forEach(printjson) यह निम्नलिखित आउटपुट उत्पन्न करेगा -
{ "StudentFullName" : "John Smith", "StudentAge" : 23 }
{ "StudentFullName" : "John Doe", "StudentAge" : 21 }
{ "StudentFullName" : "Carol Taylor", "StudentAge" : 20 }
{ "StudentFullName" : "Chris Brown", "StudentAge" : 24 }