หากต้องการสอบถามอาร์เรย์ของสตริงที่ซ้อนกัน คุณสามารถใช้เครื่องหมายจุด (.) ให้เราสร้างคอลเลกชันที่มีเอกสารก่อน -
> db.nestedStringDemo.insertOne(
{
"CustomerName": "John",
"CustomerOtherDetails": [ { "Age":29, "CountryName": "US" },
{ "CompanyName": "Amazon",
"Salary": 150000, "ProjectName": ["Online Library Management System", "Pig Dice Game"]
} ] }
);
{
"acknowledged" : true,
"insertedId" : ObjectId("5cea4629ef71edecf6a1f690")
}
> db.nestedStringDemo.insertOne(
{
"CustomerName": "Chris",
"CustomerOtherDetails": [ { "Age":27, "CountryName": "AUS" },
{ "CompanyName": "Google",
"Salary": 250000, "ProjectName": ["Chat Application", "Game Design"]
} ] }
);
{
"acknowledged" : true,
"insertedId" : ObjectId("5cea466eef71edecf6a1f691")
} ต่อไปนี้เป็นแบบสอบถามเพื่อแสดงเอกสารทั้งหมดจากคอลเลกชันโดยใช้วิธี find() -
> db.nestedStringDemo.find().pretty();
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -
{
"_id" : ObjectId("5cea4629ef71edecf6a1f690"),
"CustomerName" : "John",
"CustomerOtherDetails" : [
{
"Age" : 29,
"CountryName" : "US"
},
{
"CompanyName" : "Amazon",
"Salary" : 150000,
"ProjectName" : [
"Online Library Management System",
"Pig Dice Game"
]
}
]
}
{
"_id" : ObjectId("5cea466eef71edecf6a1f691"),
"CustomerName" : "Chris",
"CustomerOtherDetails" : [
{
"Age" : 27,
"CountryName" : "AUS"
},
{
"CompanyName" : "Google",
"Salary" : 250000,
"ProjectName" : [
"Chat Application",
"Game Design"
]
}
]
} ตอนนี้ ให้เราค้นหาอาร์เรย์ของสตริงที่ซ้อนกันโดยใช้เครื่องหมายจุด -
> db.nestedStringDemo.find({"CustomerOtherDetails.ProjectName":"Chat Application"}).pretty(); สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -
{
"_id" : ObjectId("5cea466eef71edecf6a1f691"),
"CustomerName" : "Chris",
"CustomerOtherDetails" : [
{
"Age" : 27,
"CountryName" : "AUS"
},
{
"CompanyName" : "Google",
"Salary" : 250000,
"ProjectName" : [
"Chat Application",
"Game Design"
]
}
]
}