एक संग्रह से एक दस्तावेज़ वापस करने के लिए, MongoDB में findOne() का उपयोग करें। आइए दस्तावेजों के साथ एक संग्रह बनाएं -
> db.demo463.insertOne({"StudentName":"Chris
Brown","StudentAge":21,"StudentCountryName":"US"});{
"acknowledged" : true,
"insertedId" : ObjectId("5e7f7ec8cb66ccba22cc9dcf")
}
> db.demo463.insertOne({"StudentName":"David
Miller","StudentAge":23,"StudentCountryName":"UK"});{
"acknowledged" : true,
"insertedId" : ObjectId("5e7f7ed5cb66ccba22cc9dd0")
}
> db.demo463.insertOne({"StudentName":"John
Doe","StudentAge":22,"StudentCountryName":"AUS"});{
"acknowledged" : true,
"insertedId" : ObjectId("5e7f7ee1cb66ccba22cc9dd1")
}
> db.demo463.insertOne({"StudentName":"John
Smith","StudentAge":24,"StudentCountryName":"US"});{
"acknowledged" : true,
"insertedId" : ObjectId("5e7f7eefcb66ccba22cc9dd2")
} संग्रह से सभी दस्तावेज़ों को खोजने () विधि की सहायता से प्रदर्शित करें -
> db.demo463.find();
यह निम्नलिखित आउटपुट उत्पन्न करेगा -
{ "_id" : ObjectId("5e7f7ec8cb66ccba22cc9dcf"), "StudentName" : "Chris Brown",
"StudentAge" : 21, "StudentCountryName" : "US" }
{ "_id" : ObjectId("5e7f7ed5cb66ccba22cc9dd0"), "StudentName" : "David Miller",
"StudentAge" : 23, "StudentCountryName" : "UK" }
{ "_id" : ObjectId("5e7f7ee1cb66ccba22cc9dd1"), "StudentName" : "John Doe", "StudentAge" :
22, "StudentCountryName" : "AUS" }
{ "_id" : ObjectId("5e7f7eefcb66ccba22cc9dd2"), "StudentName" : "John Smith", "StudentAge"
: 24, "StudentCountryName" : "US" } MongoDB से डेटा पुनर्प्राप्त करने की क्वेरी निम्नलिखित है -
> db.demo463.findOne({"StudentName":"John Doe"}); यह निम्नलिखित आउटपुट उत्पन्न करेगा -
{
"_id" : ObjectId("5e7f7ee1cb66ccba22cc9dd1"),
"StudentName" : "John Doe",
"StudentAge" : 22,
"StudentCountryName" : "AUS"
}