赞
踩
课程目标
MongoDB的副本集: 操作, 主要概念, 故障转移, 选举规则 MongoDB的分片集群:概念, 优点, 操作, 分片策略, 故障转移 MongoDB的安全认证
传统的关系型数据库 (比如 MySQL), 在数据操作的”三高”需求以及对应的 Web 2.0 网站需求面前, 会有”力不从心”的感觉
所谓的三高需求:
高并发, 高性能, 高可用, 简称三高
而 MongoDB 可以应对三高需求
具体的应用场景:
这些应用场景中, 数据操作方面的共同点有:
对于这样的数据, 更适合用 MongoDB 来实现数据存储
那么我们什么时候选择 MongoDB 呢?
除了架构选型上, 除了上述三个特点之外, 还要考虑下面这些问题:
99.999%
高可用如果上述有1个符合, 可以考虑 MongoDB, 2个及以上的符合, 选择 MongoDB 绝不会后悔.
MongoDB是一个开源, 高性能, 无模式的文档型数据库, 当初的设计就是用于简化开发和方便扩展, 是NoSQL数据库产品中的一种.是最 像关系型数据库(MySQL)的非关系型数据库. 它支持的数据结构非常松散, 是一种类似于 JSON 的 格式叫BSON, 所以它既可以存储比较复杂的数据类型, 又相当的灵活. MongoDB中的记录是一个文档, 它是一个由字段和值对(field:value)组成的数据结构.MongoDB文档类似于JSON对象, 即一个文档认 为就是一个对象.字段的数据类型是字符型, 它的值除了使用基本的一些类型外, 还可以包括其他文档, 普通数组和文档数组.
“最像关系型数据库的 NoSQL 数据库”. MongoDB 中的记录是一个文档, 是一个 key-value pair. 字段的数据类型是字符型, 值除了使用基本的一些类型以外, 还包括其它文档, 普通数组以及文档数组
MongoDB 数据模型是面向文档的, 所谓文档就是一种类似于 JSON 的结构, 简单理解 MongoDB 这个数据库中存在的是各种各样的 JSON(BSON)
在 MongoDB 中, 数据库和集合都不需要手动创建, 当我们创建文档时, 如果文档所在的集合或者数据库不存在, 则会自动创建数据库或者集合
操作 | 语法 |
---|---|
查看所有数据库 | show dbs; 或 show databases; |
查看当前数据库 | db; |
切换到某数据库 (若数据库不存在则创建数据库) | use <db_name>; |
删除当前数据库 | db.dropDatabase(); |
操作 | 语法 |
---|---|
查看所有集合 | show collections; |
创建集合 | db.createCollection("<collection_name>"); |
删除集合 | db.<collection_name>.drop() |
1.4.1 高性能
MongoDB 提供高性能的数据持久化
1.4.2 高可用
MongoDB 的复制工具称作副本集 (replica set) 可以提供自动故障转移和数据冗余
1.4.3 高扩展
水平扩展是其核心功能一部分
分片将数据分布在一组集群的机器上 (海量数据存储, 服务能力水平扩展)
MongoDB 支持基于片键创建数据区域, 在一个平衡的集群当中, MongoDB 将一个区域所覆盖的读写只定向到该区域的那些片
1.4.4 其他
MongoDB支持丰富的查询语言, 支持读和写操作(CRUD), 比如数据聚合, 文本搜索和地理空间查询等. 无模式(动态模式), 灵活的文档模型
默认保留的数据库
root
数据库, 如果将一个用户添加到这个数据库, 这个用户自动继承所有数据库的权限, 一些特定的服务器端命令也只能从这个数据库运行, 比如列出所有的数据库或者关闭服务器config
数据库在内部使用, 用来保存分片的相关信息- $ show dbs
-
-
- $ use articledb
-
- $ show dbs
当使用
use articledb
的时候.articledb
其实存放在内存之中, 当articledb
中存在一个 collection 之后, mongo 才会将这个数据库持久化到硬盘之中.
官方文档: https://docs.mongodb.com/manual/crud/
2.2.1 创建 Create
Create or insert operations add new documents to a collection. If the collection does not currently exist, insert operations will create the collection automatically.
db.<collection_name>.insertOne()
向集合中添加一个文档, 参数一个 json 格式的文档db.<collection_name>.insertMany()
向集合中添加多个文档, 参数为 json 文档数组- db.collection.insert({
- <document or array of documents>,
- writeConcern: <document>,
- ordered: <boolean>
- })
-
-
- // 向集合中添加一个文档
- db.collection.insertOne(
- { item: "canvas", qty: 100, tags: ["cotton"], size: { h: 28, w: 35.5, uom: "cm" } }
- )
- // 向集合中添加多个文档
- db.collection.insertMany([
- { item: "journal", qty: 25, tags: ["blank", "red"], size: { h: 14, w: 21, uom: "cm" } },
- { item: "mat", qty: 85, tags: ["gray"], size: { h: 27.9, w: 35.5, uom: "cm" } },
- { item: "mousepad", qty: 25, tags: ["gel", "blue"], size: { h: 19, w: 22.85, uom: "cm" } }
- ])
注:当我们向 collection
中插入 document
文档时, 如果没有给文档指定 _id
属性, 那么数据库会为文档自动添加 _id
field, 并且值类型是 ObjectId(blablabla)
, 就是文档的唯一标识, 类似于 relational database 里的 primary key
- mongo 中的数字, 默认情况下是 double 类型, 如果要存整型, 必须使用函数
NumberInt(整型数字)
, 否则取出来就有问题了- 插入当前日期可以使用
new Date()
如果某条数据插入失败, 将会终止插入, 但已经插入成功的数据不会回滚掉. 因为批量插入由于数据较多容易出现失败, 因此, 可以使用 try catch
进行异常捕捉处理, 测试的时候可以不处理.如:
- try {
- db.comment.insertMany([
- {"_id":"1","articleid":"100001","content":"我们不应该把清晨浪费在手机上, 健康很重要, 一杯温水幸福你我 他.","userid":"1002","nickname":"相忘于江湖","createdatetime":new Date("2019-0805T22:08:15.522Z"),"likenum":NumberInt(1000),"state":"1"},
- {"_id":"2","articleid":"100001","content":"我夏天空腹喝凉开水, 冬天喝温开水","userid":"1005","nickname":"伊人憔 悴","createdatetime":new Date("2019-08-05T23:58:51.485Z"),"likenum":NumberInt(888),"state":"1"},
- {"_id":"3","articleid":"100001","content":"我一直喝凉开水, 冬天夏天都喝.","userid":"1004","nickname":"杰克船 长","createdatetime":new Date("2019-08-06T01:05:06.321Z"),"likenum":NumberInt(666),"state":"1"},
- {"_id":"4","articleid":"100001","content":"专家说不能空腹吃饭, 影响健康.","userid":"1003","nickname":"凯 撒","createdatetime":new Date("2019-08-06T08:18:35.288Z"),"likenum":NumberInt(2000),"state":"1"},
- {"_id":"5","articleid":"100001","content":"研究表明, 刚烧开的水千万不能喝, 因为烫 嘴.","userid":"1003","nickname":"凯撒","createdatetime":new Date("2019-0806T11:01:02.521Z"),"likenum":NumberInt(3000),"state":"1"}
-
- ]);
-
- } catch (e) {
- print (e);
- }
2.2.2 查询 Read
db.<collection_name>.find()
方法对集合进行查询, 接受一个 json 格式的查询条件. 返回的是一个数组db.<collection_name>.findOne()
查询集合中符合条件的第一个文档, 返回的是一个对象可以使用 $in
操作符表示范围查询
db.inventory.find( { status: { $in: [ "A", "D" ] } } )
多个查询条件用逗号分隔, 表示 AND
的关系
db.inventory.find( { status: "A", qty: { $lt: 30 } } )
等价于下面 sql 语句
SELECT * FROM inventory WHERE status = "A" AND qty < 30
使用 $or
操作符表示后边数组中的条件是OR的关系
db.inventory.find( { $or: [ { status: "A" }, { qty: { $lt: 30 } } ] } )
等价于下面 sql 语句
SELECT * FROM inventory WHERE status = "A" OR qty < 30
联合使用 AND
和 OR
的查询语句
- db.inventory.find( {
- status: "A",
- $or: [ { qty: { $lt: 30 } }, { item: /^p/ } ]
- } )
在 terminal 中查看结果可能不是很方便, 所以我们可以用 pretty()
来帮助阅读
db.inventory.find().pretty()
匹配内容
- db.posts.find({
- comments: {
- $elemMatch: {
- user: 'Harry Potter'
- }
- }
- }).pretty()
-
- // 正则表达式
- db.<collection_name>.find({ content : /once/ })
创建索引
- db.posts.createIndex({
- { title : 'text' }
- })
-
- // 文本搜索
- // will return document with title "Post One"
- // if there is no more posts created
- db.posts.find({
- $text : {
- $search : "\"Post O\""
- }
- }).pretty()
2.2.3 更新 Update
db.<collection_name>.updateOne(<filter>, <update>, <options>)
方法修改一个匹配 <filter>
条件的文档db.<collection_name>.updateMany(<filter>, <update>, <options>)
方法修改所有匹配 <filter>
条件的文档db.<collection_name>.replaceOne(<filter>, <update>, <options>)
方法替换一个匹配 <filter>
条件的文档db.<collection_name>.update(查询对象, 新对象)
默认情况下会使用新对象替换旧对象其中 <filter>
参数与查询方法中的条件参数用法一致.
如果需要修改指定的属性, 而不是替换需要用“修改操作符”来进行修改
$set
修改文档中的制定属性其中最常用的修改操作符即为$set
和$unset
,分别表示赋值和取消赋值.
- db.inventory.updateOne(
- { item: "paper" },
- {
- $set: { "size.uom": "cm", status: "P" },
- $currentDate: { lastModified: true }
- }
- )
-
- db.inventory.updateMany(
- { qty: { $lt: 50 } },
- {
- $set: { "size.uom": "in", status: "P" },
- $currentDate: { lastModified: true }
- }
- )
- uses the $set operator to update the value of the
size.uom
field to"cm"
and the value of thestatus
field to"P"
,- uses the $currentDate operator to update the value of the
lastModified
field to the current date. IflastModified
field does not exist, $currentDate will create the field. See $currentDate for details.
db.<collection_name>.replaceOne()
方法替换除 _id
属性外的所有属性, 其<update>
参数应为一个全新的文档.
- db.inventory.replaceOne(
- { item: "paper" },
- { item: "paper", instock: [ { warehouse: "A", qty: 60 }, { warehouse: "B", qty: 40 } ] }
- )
批量修改
- // 默认会修改第一条
- db.document.update({ userid: "30", { $set {username: "guest"} } })
-
- // 修改所有符合条件的数据
- db.document.update( { userid: "30", { $set {username: "guest"} } }, {multi: true} )
列值增长的修改
如果我们想实现对某列值在原有值的基础上进行增加或减少, 可以使用 $inc
运算符来实现
db.document.update({ _id: "3", {$inc: {likeNum: NumberInt(1)}} })
修改操作符
Name | Description |
---|---|
$currentDate | Sets the value of a field to current date, either as a Date or a Timestamp. |
$inc | Increments the value of the field by the specified amount. |
$min | Only updates the field if the specified value is less than the existing field value. |
$max | Only updates the field if the specified value is greater than the existing field value. |
$mul | Multiplies the value of the field by the specified amount. |
$rename | Renames a field. |
$set | Sets the value of a field in a document. |
$setOnInsert | Sets the value of a field if an update results in an insert of a document. Has no effect on update operations that modify existing documents. |
$unset | Removes the specified field from a document. |
2.2.4 删除 Delete
db.collection.deleteMany()
方法删除所有匹配的文档.db.collection.deleteOne()
方法删除单个匹配的文档.db.collection.drop()
db.dropDatabase()
db.inventory.deleteMany( { qty : { $lt : 50 } } )
Delete operations do not drop indexes, even if deleting all documents from a collection.
一般数据库中的数据都不会真正意义上的删除, 会添加一个字段, 用来表示这个数据是否被删除
2.3.1 排序 Sort
在查询文档内容的时候, 默认是按照 _id
进行排序
我们可以用 $sort
更改文档排序规则
{ $sort: { <field1>: <sort order>, <field2>: <sort order> ... } }
For the field or fields to sort by, set the sort order to 1
or -1
to specify an ascending or descending sort respectively, as in the following example:
- db.users.aggregate(
- [
- { $sort : { age : -1, posts: 1 } }
- // ascending on posts and descending on age
- ]
- )
$sort
Operator and Memory
$sort
+ $limit
Memory Optimization
When a $sort precedes a $limit and there are no intervening stages that modify the number of documents, the optimizer can coalesce the $limit into the $sort. This allows the $sort operation to only maintain the top n
results as it progresses, where n
is the specified limit, and ensures that MongoDB only needs to store n
items in memory. This optimization still applies when allowDiskUse
is true
and the n
items exceed the aggregation memory limit.
Optimizations are subject to change between releases.
有点类似于用 heap 做 topK 这种问题, 只维护 k 个大小的 heap, 会加速 process
举个栗子:
db.posts.find().sort({ title : -1 }).limit(2).pretty()
2.3.2 投影 Projection
有些情况, 我们对文档进行查询并不是需要所有的字段, 比如只需要 id 或者 用户名, 我们可以对文档进行“投影”
1
- display0
- dont display- > db.users.find( {}, {username: 1} )
-
- > db.users.find( {}, {age: 1, _id: 0} )
> db.posts.find().forEach(fucntion(doc) { print('Blog Post: ' + doc.title) })
2.5.1 正则表达式
- $ db.collection.find({field:/正则表达式/})
-
- $ db.collection.find({字段:/正则表达式/})
2.5.2 比较查询
<
, <=
, >
, >=
这些操作符也是很常用的, 格式如下:
- db.collection.find({ "field" : { $gt: value }}) // 大于: field > value
- db.collection.find({ "field" : { $lt: value }}) // 小于: field < value
- db.collection.find({ "field" : { $gte: value }}) // 大于等于: field >= value
- db.collection.find({ "field" : { $lte: value }}) // 小于等于: field <= value
- db.collection.find({ "field" : { $ne: value }}) // 不等于: field != value
2.5.3 包含查询
包含使用 $in
操作符. 示例:查询评论的集合中 userid
字段包含 1003
或 1004
的文档
db.comment.find({userid:{$in:["1003","1004"]}})
不包含使用 $nin
操作符. 示例:查询评论集合中 userid
字段不包含 1003
和 1004
的文档
db.comment.find({userid:{$nin:["1003","1004"]}})
- 选择切换数据库:use articledb
- 插入数据:db.comment.insert({bson数据})
- 查询所有数据:db.comment.find();
- 条件查询数据:db.comment.find({条件})
- 查询符合条件的第一条记录:db.comment.findOne({条件})
- 查询符合条件的前几条记录:db.comment.find({条件}).limit(条数)
- 查询符合条件的跳过的记录:db.comment.find({条件}).skip(条数)
-
- 修改数据:db.comment.update({条件},{修改后的数据})
- 或
- db.comment.update({条件},{$set:{要修改部分的字段:数据})
-
- 修改数据并自增某字段值:db.comment.update({条件},{$inc:{自增的字段:步进值}})
-
- 删除数据:db.comment.remove({条件})
- 统计查询:db.comment.count({条件})
- 模糊查询:db.comment.find({字段名:/正则表达式/})
- 条件比较运算:db.comment.find({字段名:{$gt:值}})
- 包含查询:db.comment.find({字段名:{$in:[值1, 值2]}})
- 或
- db.comment.find({字段名:{$nin:[值1, 值2]}})
-
- 条件连接查询:db.comment.find({$and:[{条件1},{条件2}]})
- 或
- db.comment.find({$or:[{条件1},{条件2}]})
举个例子, 比如“用户-订单”这个一对多的关系中, 我们想查询某一个用户的所有或者某个订单, 我们可以
- var user_id = db.users.findOne( {username: "username_here"} )._id
- db.orders.find( {user_id: user_id} )
索引支持在 MongoDB 中高效地执行查询.如果没有索引, MongoDB 必须执行全集合扫描, 即扫描集合中的每个文档, 以选择与查询语句 匹配的文档.这种扫描全集合的查询效率是非常低的, 特别在处理大量的数据时, 查询可以要花费几十秒甚至几分钟, 这对网站的性能是非常致命的.
如果查询存在适当的索引, MongoDB 可以使用该索引限制必须检查的文档数.
索引是特殊的数据结构, 它以易于遍历的形式存储集合数据集的一小部分.索引存储特定字段或一组字段的值, 按字段值排序.索引项的排 序支持有效的相等匹配和基于范围的查询操作.此外, MongoDB 还可以使用索引中的排序返回排序结果.
MongoDB 使用的是 B Tree, MySQL 使用的是 B+ Tree
- // create index
- db.<collection_name>.createIndex({ userid : 1, username : -1 })
-
- // retrieve indexes
- db.<collection_name>.getIndexes()
-
- // remove indexes
- db.<collection_name>.dropIndex(index)
-
- // there are 2 ways to remove indexes:
- // 1. removed based on the index name
- // 2. removed based on the fields
-
- db.<collection_name>.dropIndex( "userid_1_username_-1" )
- db.<collection_name>.dropIndex({ userid : 1, username : -1 })
-
- // remove all the indexes, will only remove non_id indexes
- db.<collection_name>.dropIndexes()
4.2.1 单字段索引
MongoDB 支持在文档的单个字段上创建用户定义的升序/降序索引, 称为单字段索引 Single Field Index
对于单个字段索引和排序操作, 索引键的排序顺序(即升序或降序)并不重要, 因为 MongoDB 可以在任何方向上遍历索引.
4.2.2 复合索引
MongoDB 还支持多个字段的用户定义索引, 即复合索引 Compound Index
复合索引中列出的字段顺序具有重要意义.例如, 如果复合索引由 { userid: 1, score: -1 }
组成, 则索引首先按 userid
正序排序, 然后 在每个 userid
的值内, 再在按 score
倒序排序.
4.2.3 其他索引
地理空间索引(Geospatial Index)
为了支持对地理空间坐标数据的有效查询, MongoDB 提供了两种特殊的索引: 返回结果时使用平面几何的二维索引和返回结果时使用球面几何的二维球面索引.
文本索引(Text Indexes)
MongoDB 提供了一种文本索引类型, 支持在集合中搜索字符串内容.这些文本索引不存储特定于语言的停止词(例如 “the”, “a”, “or”), 而将集合中的词作为词干, 只存储根词.
哈希索引(Hashed Indexes)
为了支持基于散列的分片, MongoDB 提供了散列索引类型, 它对字段值的散列进行索引.这些索引在其范围内的值分布更加随机, 但只支持相等匹配, 不支持基于范围的查询.
4.3.1 索引的查看
语法
db.collection.getIndexes()
默认 _id
索引: MongoDB 在创建集合的过程中, 在 _id
字段上创建一个唯一的索引, 默认名字为 _id
, 该索引可防止客户端插入两个具有相同值的文 档, 不能在 _id
字段上删除此索引.
注意:该索引是唯一索引, 因此值不能重复, 即 _id
值不能重复的.
在分片集群中, 通常使用 _id
作为片键.
4.3.2 索引的创建
语法
db.collection.createIndex(keys, options)
参数
options(更多选项)列表
注意在 3.0.0 版本前创建索引方法为 db.collection.ensureIndex()
, 之后的版本使用了 db.collection.createIndex()
方法, ensureIndex()
还能用, 但只是 createIndex()
的别名.
举个声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/酷酷是懒虫/article/detail/864289
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。