이를 위해 find()를 사용할 수 있습니다. 먼저 문서로 컬렉션을 생성해 보겠습니다. −
> 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 } 다음은 find() 메서드를 사용하여 컬렉션의 모든 문서를 표시하는 쿼리입니다. -
> 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"
}
}
}