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