MongoDB之概述、命令
基础知识
是什么
概念
分布式文件存储数据库,提供高可用、可扩展、易部署的数据存储解决方案。
(图片来源网络,侵删)
结构
BSON存储类型
类似JSON的一种二进制存储格式。相比于JSON,提供更丰富的类型支持。
优点是灵活,缺点是空间利用率不佳。
类型 | 说明 | 解释 | 举例 |
---|---|---|---|
String | 字符串 | UTF-8编码为合法字符串。 | {name:“李四”} |
Integer | 整型 | 根据服务器可分为32、64位。 | {age:1} |
Boolean | 布尔值 | {flag:true} | |
Double | 双精度浮点值 | {number:3.14} | |
ObjectId | 对象ID | 用于创建文档的ID | {_id:new Object()} |
Array | 数组 | {top:[85,63,42]} | |
Timestamp | 时间戳 | { ts: new Timestamp() } | |
Object | 内嵌文档 | {obj:{age:18}} | |
Null | 空值 | 空值或未定义的对象 | {key:null} |
Date或者 ISODate | 格林尼治 时间 | 日期时间,用Unix日期格 式来存储当前日 期或时 间。 | {birth:new Date()} |
Code | 代码 | 可包含js代码 | {x:function(){}} |
其它特殊类型File:
- 二进制转码小于16M,可用 Base64 存储。
- 二进制转码大于16M,可用 GridFS 存储。
什么时候使用MongoDB
应用特征
应用特征 不需要事务 不需要复杂join 新应用,需求变动,数据模型无法确定,需要快速迭代开发 应用需要2000以上QPS 应用需要TB、PB级别的数据存储 应用发展迅速,需要快速水平扩展 要求数据不丢失 应用需要99.999%高可用 应用需要大量地理位置查询、文本查询 适用场景
- 网站数据:适合实时插入、更新、查询,并要求具备复制、高度伸缩性。
- 缓存:高性能,可作为基础信息设置的缓存层。
- 存储大尺寸、低价值的数据。
- 高伸缩场景。合适组成几十上百的数据库集群。内置高可用解决方案以及MapReduce引擎。
- 用于对象、JSON数据的存储。BSON格式合适文档化格式的存储和查询。
行业应用场景
- 游戏场景。用户信息、装备信息、积分等,内嵌文档存储,方便查询、更新。
- 物流场景。存储订单信息,通过内嵌文档形式,一下子就可以把订单的所有变更信息查询出来。
- 社交场景。存储用户信息、朋友圈,查找附近的人等。
- 物联网场景。存储设备的接入信息,以及设备上报的日志。
- 直播。用户信息、礼物信息。
GUI工具
MongoDB Compass Community
- MongoDB提供GUI MongoDB工具
- 借助内置模式可视化,用户可以分析文档并显示丰富的结构。为了监控服务器的负载,它提供了数据库操作的实时统计信息
- Compass有两个版本:Enterprise(付费),Community(免费)
- 适用于Linux,Mac或Windows
MongoBooster
(图片来源网络,侵删)- 是MongoDB CLI界面中非常流行的GUI工具。它正式名称为MongoBooster
- NoSQLBooster是一个跨平台,它带有一堆MongoDB 工具来管理数据库和监控服务器
- 这个Mongodb工具包括服务器监控工具,Visual Explain Plan,查询构建器,SQL查询,
- ES2017语法支持等等…
- 适用于Linux,Mac或Windows
Navicat
基础命令
连接
mongo --username root --password --host localhost --port 27017
数据库
当前数据库
db
所有数据库
show dbs
show databases
切换数据库
use
创建数据库
use
(图片来源网络,侵删)删除当前数据库
db.dropDatabase()
集合操作
所有集合(表)
show collections
show tables
查看集合命令帮助
db..help()
删除集合
db.. drop()
插入操作
单条插入
db..insertOne( {xm:"张三",age:23} ) db.myt.insertOne( {xm:"张三",age:23} )
多条插入
db..insertMany([ {xxx}, {xxx} ]) db.myt.insertMany([ {xm:"李四",age:24}, {xm:"王五",age:25}, {xm:"赵六",age:26}, {xm:"李四",age:34}, {xm:"王五",age:35}, {xm:"赵六",age:36} ])
查询操作
基础查询
操作 格式 对比 = {key:value} where a = 1 > {key:{$gt:value}} where a > 1 = {key:{$gte:value}} where a >= 1 like:{$lt:20}}).count() db.authors.distinct("like") "$match": {"like": {"$gt" : 10} }} ) "$match": {"like": {"$gte" : 25} }}, {"$group": {"_id": "$author", "count": {"$sum": 1}}} ) "$match": {"like": {"$gte" : 25} }}, {"$group": {"_id": {a:"$author",b:"$like"}, "count": {"$sum": 1}}} ) "$group": {"_id": "$author", "count": {"$max": "$like"}}} ) "$group": {"_id": "$author", "count": {"$avg": "$like"}}} ) "$group": {"_id": "$author", "like": {"$addToSet": "$like"}}} ) { "_id" : "Vincent", "like" : [ 40, 10 ] } { "_id" : "yilia", "like" : [ 8 ] } { "_id" : "della", "like" : [ 30 ] } { "_id" : "benson", "like" : [ 20 ] } { "_id" : "louise", "like" : [ 30 ] } "$group": {"_id": "$author", "like": {"$push": "$like"}}} ) { "_id" : "Vincent", "like" : [ 10, 40, 10 ] } { "_id" : "yilia", "like" : [ 8 ] } { "_id" : "della", "like" : [ 30 ] } { "_id" : "benson", "like" : [ 20 ] } { "_id" : "louise", "like" : [ 30 ] } "$match": {"like": {"$gte" : 10} }}, {"$project": {"_id": 0, "author":1, "title": 1}} ) -- 重命名 db.authors.aggregate( {"$match": {"like": {"$gte" : 10} }}, {"$project": {"_id": 0, "author":1, "B-Name": "$title"}} ) "$match": {"like": {"$gte" : 10} }}, {"$group": {"_id": "$author", "count": {"$sum": 1}}}, {"$sort": {"count": -1}}, {"$limit": 1} ) "$project": {"newLike": {"$add": ["$like", 1]}}} ) { "_id" : ObjectId("63b2b63e653186ee23d724b3"), "newLike" : 11 } { "_id" : ObjectId("63b2b63e653186ee23d724b4"), "newLike" : 31 } { "_id" : ObjectId("63b2b63e653186ee23d724b5"), "newLike" : 21 } { "_id" : ObjectId("63b2b63e653186ee23d724b6"), "newLike" : 41 } { "_id" : ObjectId("63b2b63e653186ee23d724b7"), "newLike" : 31 } { "_id" : ObjectId("63b2b63e653186ee23d724b8"), "newLike" : 9 } { "_id" : ObjectId("63b2d5a7653186ee23d724b9"), "newLike" : 11 } "$project": {"newLike": {"$subtract": ["$like", 2]}}} ) { "_id" : ObjectId("63b2b63e653186ee23d724b3"), "newLike" : 8 } { "_id" : ObjectId("63b2b63e653186ee23d724b4"), "newLike" : 28 } { "_id" : ObjectId("63b2b63e653186ee23d724b5"), "newLike" : 18 } { "_id" : ObjectId("63b2b63e653186ee23d724b6"), "newLike" : 38 } { "_id" : ObjectId("63b2b63e653186ee23d724b7"), "newLike" : 28 } { "_id" : ObjectId("63b2b63e653186ee23d724b8"), "newLike" : 6 } { "_id" : ObjectId("63b2d5a7653186ee23d724b9"), "newLike" : 8 } "$project": {"newLike": {"$multiply": ["$like", 10]}} } ) { "_id" : ObjectId("63b2b63e653186ee23d724b3"), "newLike" : 100 } { "_id" : ObjectId("63b2b63e653186ee23d724b4"), "newLike" : 300 } { "_id" : ObjectId("63b2b63e653186ee23d724b5"), "newLike" : 200 } { "_id" : ObjectId("63b2b63e653186ee23d724b6"), "newLike" : 400 } { "_id" : ObjectId("63b2b63e653186ee23d724b7"), "newLike" : 300 } { "_id" : ObjectId("63b2b63e653186ee23d724b8"), "newLike" : 80 } { "_id" : ObjectId("63b2d5a7653186ee23d724b9"), "newLike" : 100 } "$project": {"newLike": {"$divide": ["$like", 10]}} } ) "$project": {"newLike": {"$mod": ["$like", 3]}} } ) "$project": {"newTitle": {"$substr": ["$title", 1, 2] } }} ) "$project": {"newTitle": {"$concat": ["$title", "(", "$author", ")"] } }} ) "$project": {"newTitle": {"$toLower": "$title"} }} ) -- 大写 db.authors.aggregate( {"$project": {"newAuthor": {"$toUpper": "$author"} }} ) "$set": {"publishDate": new Date()}}, true, true ) -- 查询月份 db.authors.aggregate( {"$project": {"month": {"$month": "$publishDate"}}} ) "$project": {"result": {"$cmp": ["$like", 20]} }} ) "$project": { "result": { "$or": [{"$eq": ["$author", "Vincent"]}, {"$gt": ["$like",20]}] } } } ) "$project": {"result": {"$not": {"$eq": ["$author", "Vincent"]}}}} ) "$project": { "result": {"$cond": [ {"$eq": ["$author", "Vincent"]}, "111", "222" ]}} } )
文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。
还没有评论,来说两句吧...