查詢 MongoDB 中的巢狀字串陣列?
要查詢巢狀字串陣列,可以使用點(.)符號。我們先使用文件建立一個集合——
> 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"
]
}
]
}
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP