本文介紹如何在ApsaraDB for MongoDB中建立資料庫和集合并寫入資料。
前提條件
已根據快速入門步驟成功串連執行個體。
操作步驟
DMS
建立
test
資料庫。輸入以下命令,單擊執行。
use test
成功建立資料庫後,單擊執行結果中的資料庫名,跳轉至目標資料庫環境。
在
test
資料庫中建立mongo
集合。輸入以下命令,單擊執行。如彈出執行確認對話方塊中,單擊確認。
db.createCollection("mongo")
返回結果中
ok
取值為1.0
時,表示建立成功,其他取值表示建立失敗。將兩組資料
{"name": "test"}
和{"count": "10"}
寫入mongo
集合。輸入以下命令,單擊執行。如彈出執行確認對話方塊中,單擊確認。
db.runCommand({insert: "mongo", documents: [{"name": "test"},{"count": "10"}]})
查詢
mongo
集合中的資料。輸入以下命令,單擊執行。
db.getCollection("mongo").find({})
MongoDB Shell
建立並切換至
test
資料庫。use test
在
test
資料庫中建立mongo
集合。db.createCollection("mongo")
返回結果中
ok
取值為1.0
時,表示建立成功,其他取值表示建立失敗。將兩組資料
{"name": "test"}
和{"count": "10"}
寫入mongo
集合。db.runCommand({insert: "mongo", documents: [{"name": "test"},{"count": "10"}]})
查詢
mongo
集合中的資料。db.getCollection("mongo").find({})