全部產品
Search
文件中心

ApsaraDB for MongoDB:寫入資料

更新時間:Jun 04, 2025

本文介紹如何在ApsaraDB for MongoDB中建立資料庫和集合并寫入資料。

前提條件

已根據快速入門步驟成功串連執行個體

操作步驟

DMS

  1. 建立test資料庫。

    輸入以下命令,單擊執行

    use test

    2025-05-14_16-57-43

    成功建立資料庫後,單擊執行結果中的資料庫名,跳轉至目標資料庫環境。

  2. test資料庫中建立mongo集合。

    輸入以下命令,單擊執行。如彈出執行確認對話方塊中,單擊確認

    db.createCollection("mongo")

    返回結果中ok取值為1.0時,表示建立成功,其他取值表示建立失敗。2025-05-14_17-01-08

  3. 將兩組資料{"name": "test"}{"count": "10"}寫入mongo集合。

    輸入以下命令,單擊執行。如彈出執行確認對話方塊中,單擊確認

    db.runCommand({insert: "mongo", documents: [{"name": "test"},{"count": "10"}]})

    2025-05-14_17-09-44

  4. 查詢mongo集合中的資料。

    輸入以下命令,單擊執行

    db.getCollection("mongo").find({})

    2025-05-14_17-01-08

MongoDB Shell

  1. 建立並切換至test資料庫。

    use test

    1

  2. test資料庫中建立mongo集合。

    db.createCollection("mongo")

    返回結果中ok取值為1.0時,表示建立成功,其他取值表示建立失敗。

    2

  3. 將兩組資料{"name": "test"}{"count": "10"}寫入mongo集合。

    db.runCommand({insert: "mongo", documents: [{"name": "test"},{"count": "10"}]})

    3

  4. 查詢mongo集合中的資料。

    db.getCollection("mongo").find({})

    4