写入数据

本文介绍如何在云数据库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

    OSZAR »