आप इसके लिए खोज () का उपयोग कर सकते हैं। आइए पहले दस्तावेजों के साथ एक संग्रह बनाएं -
> db.findInDictionaryDemo.insertOne(
... {
... "_id":101,
... "AllCustomerDetails":
... {
... "SomeCustomerDetail1":
... {
... "CustomerName1":"John Doe",
... "CustomerName2":"John Smith"
... },
... "SomeCustomerDetail2":
... {
... "CustomerName1":"Carol Taylor",
... "CustomerName2":"David Miller"
... }
... }
... }
... );
{ "acknowledged" : true, "insertedId" : 101 }
> db.findInDictionaryDemo.insertOne(
... {
... "_id":102,
... "AllCustomerDetails":
... {
... "SomeCustomerDetail1":
... {
... "CustomerName1":"Sam Wiliams",
... "CustomerName2":"Bob Johnson"
... },
... "SomeCustomerDetail2":
... {
... "CustomerName1":"Chris Brown",
... "CustomerName2":"Mike Wilson"
... }
... }
... }
... );
{ "acknowledged" : true, "insertedId" : 102 } खोज () विधि की मदद से संग्रह से सभी दस्तावेजों को प्रदर्शित करने के लिए क्वेरी निम्नलिखित है -
> db.findInDictionaryDemo.find().pretty();
यह निम्नलिखित आउटपुट देगा -
{
"_id" : 101,
"AllCustomerDetails" : {
"SomeCustomerDetail1" : {
"CustomerName1" : "John Doe",
"CustomerName2" : "John Smith"
},
"SomeCustomerDetail2" : {
"CustomerName1" : "Carol Taylor",
"CustomerName2" : "David Miller"
}
}
}
{
"_id" : 102,
"AllCustomerDetails" : {
"SomeCustomerDetail1" : {
"CustomerName1" : "Sam Wiliams",
"CustomerName2" : "Bob Johnson"
},
"SomeCustomerDetail2" : {
"CustomerName1" : "Chris Brown",
"CustomerName2" : "Mike Wilson"
}
}
} MongoDB में मूल्य द्वारा शब्दकोश में खोजने के लिए क्वेरी निम्नलिखित है -
>db.findInDictionaryDemo.find({"AllCustomerDetails.SomeCustomerDetail2.CustomerName2":"Mike Wilson"}).pretty(); यह निम्नलिखित आउटपुट देगा -
{
"_id" : 102,
"AllCustomerDetails" : {
"SomeCustomerDetail1" : {
"CustomerName1" : "Sam Wiliams",
"CustomerName2" : "Bob Johnson"
},
"SomeCustomerDetail2" : {
"CustomerName1" : "Chris Brown",
"CustomerName2" : "Mike Wilson"
}
}
}