[英 => 中] MongoDB 起步

原文地址

起步

是一个针对供应,运行,监测,维护 MongoDB 部署的云托管服务。它是入门 MongoDB 快速,简单,免费的一个方式。想在本地安装和运行 MongoDB,查阅

学习关于 MongoDB 查询语言和其它 MongoDB 原理,注册

1 创建一个 Altas 用户账户

开始学习 MongoDB Atlas,创建你的用户账户并登录 Atlas。

2 创建一个 Atlas Group

给你的 group 选择一个名字。

atlas-create-group

Groups 管理相关的 MongoDB 部署。创建额外的 Atlas groups,点击右上角你的用户名并选择 My Groups。点击 Add Group 按钮。

3 创建一个 cluster

MongoDB 部署,或 Atlas 中的 “clusters”,可以是

创建 cluster,进入 Clusters 视图并点击 Add New ClusterBuild a New Cluster 按钮。

atlas-create-cluster

1.输入 Cluster Name
2.点击 Instance Size M0Select 按钮使用 Atlas Free Tier
3.输入 UsernamePassword

atlas-create-cluster-add-user

这些字段只在你的 Atlas group 没有 MongoDB 用户存在时出现。如果你事先在 group 创建了用户这些不会出现。

4.点击 Confirm & Deploy

4 设置安全功能

Atlas 只允许从 group's whitelist 中的客户端连接到 cluster。添加入口到 whitelist:

atlas-setup-cluster-security

1.从 Clusters 视图点击 Security 标签
2.点击 IP Whitelist,然后点击 Add IP Address。你在 Whitelist Entry 字段中可以手动输入 IP 地址或点击 Add Current IP Address 按钮
3.点击 Confirm 并等待 Atlas 更新防火墙

5 下载连接客户端

下载 Shell:

1.在 Clusters 视图点击 Connenct 按钮
2.在 Connect with the Mongo Shell 下的部分,从下拉菜单中选择你的操作系统并点击 Download Shell
3.解压下载的文件到你期望的位置或根据你的操作系统安装 mongo shell
4.选择性地添加 /bin 到你的操作系统的 PATH 或环境变量中

需要更多操作系统具体的安装信息,查阅

6 获取 URI 连接字符串

从 MongoDB Atlas Clusters 视图:

1.点击 Connect 按钮查看
2.复制连接字符串到剪贴板
3.黏贴连接字符串到某处并用之前创建的用户密码替换 ,用你想要连接的数据库名字替换 。下面的例子中,使用 test 数据库。

7 连接 cluster

使用上一步的 URI string,连接到 Atlas cluster:

/bin/mongo "mongodb://user123:p455w0rd@gettingstarted-shard-00-00-hyjsm.mongodb.net:27017,gettingstarted-shard-00-01-hyjsm.mongodb.net:27017,gettingstarted-shard-00-02-hyjsm.mongodb.net:27017/test?ssl=true&replicaSet=GettingStarted-shard-0&authSource=admin"

如果你添加了 /binPATH 你也可以运行:

Windows 中,运行:

Documents 和 Collections

MongoDB 在 中以 存储数据。MongoDB 数据库存储 documents 的 collections。

Insert Documents

能 insert 多个 documents 到 collection。给这个方法传入一个 documents 到数组。

下面的示例 inserts 新的 documents 到 inventory collection:

db.inventory.insertMany([
   // MongoDB adds the _id field with an ObjectId if _id is not present
   { item: "journal",qty: 25,status: "A",size: { h: 14,w: 21,uom: "cm" },tags: [ "blank","red" ] },{ item: "notebook",qty: 50,size: { h: 8.5,w: 11,uom: "in" },tags: [ "red","blank" ] },{ item: "paper",qty: 100,status: "D","blank","plain" ] },{ item: "planner",qty: 75,size: { h: 22.85,w: 30,{ item: "postcard",qty: 45,size: { h: 10,w: 15.25,tags: [ "blue" ] }
]);

insertMany() 返回一个包含最新 inserted 的 documents _id 字段值。查阅 示例。

使用 insert 单个 document。

更多信息和示例,查阅 部分的

Query Documents

选择所有 Documents

在 collection 中选择所有 documents,给 方法传入空的 document 作为

db.inventory.find( {} )

要查询特定的 documents,给 find() 方法传入一个要查询的 documents,形式为 : 。下面的示例从 inventory collection 查询所有 status 等于 “D” 的 documents。

db.inventory.find( { status: "D" } )

匹配 Embedded Document

在整个 embedded document 上的 equality matches 需要指定 document 的 exact match,包括字段顺序。比如,下面的查询选择所有字段 size 等于 document { h: 14,uom: "cm" } 的 document:

db.inventory.find( { size: { h: 14,uom: "cm" } } )

在 Embedded Document 中匹配一个字段

下面的示例选择所有 sizeuom 等于 “in” 的 documents:

db.inventory.find( { "size.uom": "in" } )

匹配数组中的元素

下面的示例查询所有 tags 数组中包含 “red” 元素的 documents:

db.inventory.find( { tags: "red" } )

精确匹配数组

下面的示例精确查询所有 tags 数组为指定顺序的 2 个元素 “red” 和 “blank” 的 document:

db.inventory.find( { tags: ["red","blank"] } )

更多信息和查询示例,查阅 部分的

要更新或删除 documents,查阅

相关文章

文章浏览阅读552次。com.mongodb.MongoQueryException: Quer...
文章浏览阅读635次,点赞9次,收藏8次。MongoDB 是一种 NoSQ...
文章浏览阅读2.1k次。和。_mongodb 日期类型
文章浏览阅读1.7k次。Scalestack等客户期待使用MongoDB Atla...
文章浏览阅读970次。SpringBoot整合中间件mongodb、ES_sprin...
文章浏览阅读673次。MongoDB 简介_尚医通sql