查询 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