MongoDB में _id द्वारा किसी दस्तावेज़ को खोजने के लिए, आपको ObjectId () को कॉल करने की आवश्यकता है। आइए सबसे पहले सिंटैक्स देखें
db.yourCollectionName.find({"_id":ObjectId("yourId")}).pretty(); अवधारणा को समझने और किसी दस्तावेज़ को खोजने के लिए, आइए हम दस्तावेज़ों के साथ एक संग्रह बनाने के लिए निम्नलिखित क्वेरी को लागू करें
> db.searchDocumentDemo.insertOne({"UserId":1,"UserName":"Larry"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c97a8e4330fd0aa0d2fe487")
}
> db.searchDocumentDemo.insertOne({"UserId":2,"UserName":"Mike"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c97a8ea330fd0aa0d2fe488")
}
> db.searchDocumentDemo.insertOne({"UserId":3,"UserName":"David"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c97a8f1330fd0aa0d2fe489")
}
> db.searchDocumentDemo.insertOne({"UserId":4,"UserName":"Chris"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c97a8fa330fd0aa0d2fe48a")
}
> db.searchDocumentDemo.insertOne({"UserId":5,"UserName":"Robert"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c97a901330fd0aa0d2fe48b")
}
> db.searchDocumentDemo.insertOne({"UserId":6,"UserName":"Sam"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c97a911330fd0aa0d2fe48c")
} एक संग्रह से सभी दस्तावेज़ों को खोजने () विधि की सहायता से प्रदर्शित करने के लिए क्वेरी निम्नलिखित है
> db.searchDocumentDemo.find().pretty();
This will produce the following output:
{
"_id" : ObjectId("5c97a8e4330fd0aa0d2fe487"),
"UserId" : 1,
"UserName" : "Larry"
}
{
"_id" : ObjectId("5c97a8ea330fd0aa0d2fe488"),
"UserId" : 2,
"UserName" : "Mike"
}
{
"_id" : ObjectId("5c97a8f1330fd0aa0d2fe489"),
"UserId" : 3,
"UserName" : "David"
}
{
"_id" : ObjectId("5c97a8fa330fd0aa0d2fe48a"),
"UserId" : 4,
"UserName" : "Chris"
}
{
"_id" : ObjectId("5c97a901330fd0aa0d2fe48b"),
"UserId" : 5,
"UserName" : "Robert"
}
{
"_id" : ObjectId("5c97a911330fd0aa0d2fe48c"),
"UserId" : 6,
"UserName" : "Sam"
} MongoDB में _id द्वारा दस्तावेज़ खोजने की क्वेरी निम्नलिखित है। हमने परिणाम प्रदर्शित करने के लिए ObjectId() को कॉल किया:
> db.searchDocumentDemo.find({"_id":ObjectId("5c97a901330fd0aa0d2fe48b")}).pretty(); यह निम्नलिखित आउटपुट उत्पन्न करेगा:
{
"_id" : ObjectId("5c97a901330fd0aa0d2fe48b"),
"UserId" : 5,
"UserName" : "Robert"
}