設定文件資料庫
使用 Amazon DocumentDB (與 MongoDB 相容) 和 AWS Cloud9
Amazon DocumentDB (與 MongoDB 相容) 是一種快速、可擴展、高度可用且全受管的文件資料庫服務,可支援 MongoDB 工作負載,且可輕鬆儲存、查詢和索引 JSON 資料。
此教學向您展示了如何在 AWS Cloud9 下開始使用 Amazon DocumentDB。您將學習如何使用 mongo shell 從 AWS Cloud9 環境連線至 Amazon DocumentDB 叢集,並執行一些查詢。
完成此演練費用為 0.08 USD。
以下圖表展示此演練的最終架構。

關於本教學 | |
---|---|
時間 | 10 分鐘之內 |
費用 | Amazon DocumentDB T3 中型執行個體的教學費用大約為 0.08 USD。如需詳細資訊,請參閱 Amazon DocumentDB 定價。 AWS Cloud9 為免費方案。如需詳細資訊,請參閱 AWS Cloud9 定價。 |
使用案例 | 資料庫 |
產品 | Amazon DocumentDB (與 MongoDB 相容)、AWS Cloud9 |
對象 | 開發人員 |
等級 | 200:中級。 此內容專注於提供 AWS 服務或功能概觀,並假設客戶具有該主題的應用知識。 |
上次更新日期 | 2020 年 8 月 |
1.建立 AWS Cloud9 環境
1.1 – 使用 AWS 管理主控台,在 AWS Cloud9 管理主控台上,選取 Create environment (建立環境)。

1.2 – 輸入名稱 DocumentDBCloud9。

1.3 – 選擇 Next step (下一步)。

1.4 – 在 Configure Settings (配置設定) 區段,接受所有預設值。
1.5 – 選擇 Next step (下一步)。
1.6 – 在 Review (檢閱) 區段,選擇 Create environment (建立環境)。

1.7 – 佈建 AWS Cloud9 環境需要 3 分鐘。
2.設定安全群組
2.1 – 在 Amazon EC2 管理主控台的 Network & Security (網路和安全) 下,選擇 Security groups (安全群組)。
2.2 – 選擇 Create security group (建立安全群組)。
2.3 – 針對 Security group name (安全群組名稱),輸入 demoDocDB。
2.4 – 針對 Description (描述),輸入描述。
2.5 – 針對 VPC,接受使用預設 VPC
2.6 – 在 Inbound rules (傳入規則) 區段,選擇 Add rule (新增規則)。
2.7 – 針對 Type (類型),選擇 Custom TCP Rule (自訂 TCP 規則)。
2.8 – 在 Port Range (連接埠範圍) 中,輸入 27017。
2.9 – 來源安全群組是您剛建立的 AWS Cloud9 環境的安全群組。保留 Source (來源) 為 Custom (自訂) 的預設值,在 Custom (自訂) 旁的欄位中輸入 "cloud9",以查看可用安全群組清單。

2.10 – 選擇名稱為 aws-cloud9-<environment name> 的安全群組
2.11 – 接受所有其他預設值並選擇 Create security group (建立安全群組)。您無需修改傳出規則。
以下螢幕擷取畫面向您展示了此步驟中建立的安全群組和您在建立 AWS Cloud9 環境時建立的 AWS Cloud9 安全群組。

3.建立 Amazon DocumentDB 叢集
3.1 – 在 Amazon DocumentDB 管理主控台的 Clusters (叢集) 下,選擇 Create (建立)。

3.2 – 在 Create Amazon DocumentDB cluster (建立 Amazon DocumentDB 叢集) 頁面的 Instance class (執行個體類別) 下選取 db.t3.medium ,然後針對 Number of instances (執行個體數目) 選擇 1。這些選項將協助降低成本。

3.3 – 保留其他設定為預設值。
3.4 – 在 Authentication (驗證) 區段,輸入使用者名稱和密碼。

3.5 – 開啟 Show advanced settings (顯示進階設定)。

3.6 – 在 Network settings (網路設定) 區段,為 VPC security groups (VPC 安全群組) 選擇 demoDocDB。

3.7 – 選擇 Create cluster (建立叢集)。
Amazon DocumentDB 目前正在佈建您的叢集,可能需要幾分鐘才能完成。當叢集和執行個體狀態顯示為 "Available" (可用) 時,您可以連接至叢集。Amazon DocumentDB 佈建叢集時,請完成其餘步驟,以連接至 Amazon DocumentDB 叢集。
4.安裝 mongo shell
4.1 – 如果您的 AWS Cloud9 環境仍處於開啟狀態,則可以跳至步驟 3。
4.2 – 在 AWS Cloud9 管理主控台的 Your environments (您的環境) 下,選擇 DocumentDBCloud9。
4.3 – 選擇 open IDE (開啟 IDE)。
4.4 – 在命令提示字元,使用以下程式碼輸入儲存庫檔案:
echo -e "[mongodb-org-3.6] \nname=MongoDB Repository\nbaseurl=https://repo.mongodb.org/yum/amazon/2013.03/mongodb-org/3.6/x86_64/\ngpgcheck=1 \nenabled=1 \ngpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc" | sudo tee /etc/yum.repos.d/mongodb-org-3.6.repo
4.5 – 完成時,使用以下程式碼安裝 mongo shell:
sudo yum install -y mongodb-org-shell
4.6 – 若要在途加密資料,請下載適用於 Amazon DocumentDB 的憑證授權機構憑證。查看以下程式碼:
wget https://s3.amazonaws.com/rds-downloads/rds-combined-ca-bundle.pem
4.7 – 您現在可以連接至 Amazon DocumentDB 叢集。
5.連接至 Amazon DocumentDB 叢集
5.1 – 在 Amazon DocumentDB 管理主控台的 Clusters (叢集) 下,找到您的叢集。本文使用叢集 docdb-2020-02-08-14-15-11。

5.2 – 透過按一下叢集識別碼 (例如,本例中的 docdb-2020-02-08-14-15-11) 選擇您建立的叢集。
5.3 – 複製 "Connect to this cluster with the mongo shell" (使用 mongo shell 連接至此叢集) 下提供的連線字串
忽略 <insertYourPassword> ,這樣 mongo shell 會在您連線時提示輸入密碼。這樣,您無需以純文字輸入密碼。

5.4 – 您的連線字串應類似以下程式碼 (查看螢幕擷取畫面)

5.5 – 當您輸入密碼並看見 rs0:PRIMARY> 提示字元時,則已成功連接至 Amazon DocumentDB 叢集。
如需有關疑難排解的資訊,請參閱疑難排解 Amazon DocumentDB。
6.插入並查詢資料
6.1 - 現在您已連接至叢集,您可以執行一些查詢來熟悉文件資料庫的使用。
若要插入單個文件,請輸入以下程式碼:
db.collection.insert({"hello":"DocumentDB"})
得到以下輸出:
WriteResult({ "nInserted" : 1 })
6.2 - 您可以閱讀以 findOne() 命令撰寫的文件 (因為它僅傳回單個文件)。查看以下程式碼:
db.collection.findOne()
得到以下輸出:
{ "_id" : ObjectId("5e401fe56056fda7321fbd67"), "hello" : "DocumentDB" }
6.3 - 若要執行更多查詢,請考量遊戲設定檔使用案例。首先,將一些項目插入名稱為設定檔的集合。查看以下程式碼:
db.profiles.insertMany([
{ "_id" : 1, "name" : "Tim", "status": "active", "level": 12, "score":202},
{ "_id" : 2, "name" : "Justin", "status": "inactive", "level": 2, "score":9},
{ "_id" : 3, "name" : "Beth", "status": "active", "level": 7, "score":87},
{ "_id" : 4, "name" : "Jesse", "status": "active", "level": 3, "score":27}
])
得到以下輸出:
{ "acknowledged" : true, "insertedIds" : [ 1, 2, 3, 4 ] }
6.4 -使用 find() 命令傳回設定檔集合中的所有文件。查看以下程式碼:
db.profiles.find()
得到以下輸出:
{ "_id" : 1, "name" : "Tim", "status" : "active", "level" : 12, "score" : 202 }
{ "_id" : 2, "name" : "Justin", "status" : "inactive", "level" : 2, "score" : 9 }
{ "_id" : 3, "name" : "Beth", "status" : "active", "level" : 7, "score" : 87 }
{ "_id" : 4, "name" : "Jesse", "status" : "active", "level" : 3,
6.5 - 使用篩選器對單個文件進行查詢。查看以下程式碼。
db.profiles.find({name: "Jesse"})
得到以下輸出:
{ "_id" : 4, "name" : "Jesse", "status" : "active", "level" : 3, "score" : 27 }
6.6 - 遊戲的一個常見使用案例是尋找指定使用者的設定檔,並在該使用者的設定檔內增加一個值。在此情況下,您想要針對最活躍的遊戲玩家執行促銷。如果遊戲玩家填寫意見調查,則您將其得分增加 10 分。
若要這樣做,請使用 findAndModify 命令。在此使用案例中,使用者 Tim 收到並完成意見調查。若要給 Tim 的得分加上積分,請輸入以下程式碼:
db.profiles.findAndModify({
query: { name: "Tim", status: "active"},
update: { $inc: { score: 10 } }
})
得到以下輸出:
{
"_id" : 1,
"name" : "Tim",
"status" : "active",
"level" : 12,
"score" : 202
}
6.7 - 您可以使用以下查詢驗證結果:
db.profiles.find({name: "Tim"})
得到以下輸出:
{ "_id" : 1, "name" : "Tim", "status" : "active", "level" : 12, "score" : 212 }
7.清理
恭喜
此教學向您展示了如何透過建立 AWS Cloud9 環境開始使用 Amazon DocumentDB。
您能夠安裝 mongo shell,建立 Amazon DocumentDB 叢集,連接至叢集,然後執行一些查詢,瞭解在 Amazon DocumentDB 內插入和查詢 JSON 文件有多容易。
Amazon DocumentDB (與 MongoDB 相容) 是一種快速、可擴展、高度可用且全受管的文件資料庫服務,可支援 MongoDB 工作負載,且可輕鬆儲存、查詢和索引 JSON 資料。