MongoDB 集合中的特定于项目的数组字段?
我们首先创建一个集合,其中包含文档 -
> db.projectionAnElementDemo.insertOne(
... {
... "CustomerId":100,
... "CustomerDetails": [
... {
... "CustomerName": "Chris",
... "CustomerCountryName": "US"
... },
... {
... "CustomerName": "Robert",
... "CustomerCountryName": "UK"
... }
... ]
... }
... );
{
"acknowledged" : true,
"insertedId" : ObjectId("5cd31c56b64f4b851c3a13ea")
}以下是使用 find() 方法显示集合中所有文档的查询 -
> db.projectionAnElementDemo.find().pretty();
这将会产生以下输出 -
{
"_id" : ObjectId("5cd31c56b64f4b851c3a13ea"),
"CustomerId" : 100,
"CustomerDetails" : [
{
"CustomerName" : "Chris",
"CustomerCountryName" : "US"
},
{
"CustomerName" : "Robert",
"CustomerCountryName" : "UK"
}
]
}以下是投影数组字段中的元素的查询 -
> db.projectionAnElementDemo.find({},{CustomerId:1, "CustomerDetails.CustomerName":1}).pretty();这将会产生以下输出 -
{
"_id" : ObjectId("5cd31c56b64f4b851c3a13ea"),
"CustomerId" : 100,
"CustomerDetails" : [
{
"CustomerName" : "Chris"
},
{
"CustomerName" : "Robert"
}
]
}
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
JavaScript
PHP