本文介绍如何在云数据库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({})
该文章对您有帮助吗?