SQL和MongoDB对比:
一、关于database的基础命令
查看当前的数据库:db
查看所有的数据库:show dbs / show databases
切换数据库:use db_name
删除当前的数据库:db.dropDatabase()
二、关于集合的基础命令
在MongoDB中,没有表这一概念,集合就相当于它的数据表。其中,MongoDB中不需要手动创建集合(向不存在的集合中第一次加入数据时,集合就会被创建出来)
(1)手动创建集合:
- db.createCollection(name,options)
- db.createCollection("stu")
- db.createCollection("sub",{capped:true,size:10})
参数capped:默认值为false表示不设置上限,值为true表示设置上限( 先进先出的队列)
参数size:当capped值为true时,需要指定此参数,表示上限大小,当文档达到上限时,会将之前的数据覆盖掉,单位为字节
(2)查看集合:show collections
(3)删除集合:db.集合名称.drop()
三、MongoDB的数据类型
Object ID:文档ID
String:字符串,最常用,必须是有效的UTF-8
Boolean:存储一个布尔值,true或false
Integer:整数可以是32位或者64位,这取决于服务器
Double:存储浮点值
Arrays:数组或者列表,多个值存储到一个键
Object:用于嵌入式的文档,即一个值为一个文档
Null:存储Null值
Timestamp:时间戳,表示1970-1-1到现在的总秒数
Date:存储当前日期挥着时间的UNIX时间格式
四、MongoDB的增删改查
1、增加数据
首先,切换到需要操作的数据库,选择对应需要插入的集合
> db.hhh.insert({"name":"hanmh", "age":21})
WriteResult({ "nInserted" : 1 })
(1)插入的另一种方法——save
- db.集合名称.save(document)
- 如果文档的_id(键值)已经存在则修改内部对应的值,如果文档的_id不存在则添加
2、查找对应集合中的数据
使用find命令:
- > db.hhh.find()
- { "_id" : ObjectId("5fcba0e0796e7dccbbbfb6b8"), "name" : "hanmh", "age" : 21 }
3、更新数据
$set:{键:值} 指定单个键对应的值
4、MongoDB删除操作
db.collectionName.remove({"name":“hanmh”},{justOne:true})
注:默认情况下(false),会删除所有满足条件的数据
5、MongoDB的高级查询
方法find():查询
- db.集合名称.find({条件文档})
方法findOne():查询,只返回第一个
- db.集合名称.findOne({条件文档})
方法pretty():将结果格式化
- db.集合名称.find({条件文档}).pretty()
- > db.hhh.find({age:{$gte:50}}).pretty()
{
"_id" : ObjectId("5fcc3f5ee912c7446eb39006"),
"name" : "hanmh1",
"age" : 99
}
(1)比较运算符的使用
- 等于:默认是等于判断,没有运算符
- 小于:$lt(less than)
- 小于等于:$lte(less than equal)
- 大于:$gt(greater than)
- 大于等于:$gte
- 不等于:$ne
注:简单使用,db.stu.find({age:{$gte:18}})
(2)范围操作
使用$in,$in 判断是否在某个范围内
注:简单使用,db.stu({age:{$in:[16,18,20}})
(3)逻辑运算符
and:在json中写多个条件即可
- 查询名字是hanmh1,且年龄大于等于30的人
- > db.hhh.find({"name":"hanmh1","age":{$gte:30}})
- { "_id" : ObjectId("5fcc3f5ee912c7446eb39006"), "name" : "hanmh1", "age" : 99 }
- { "_id" : ObjectId("5fcc3fb1e912c7446eb3900b"), "name" : "hanmh1", "age" : 40 }
or:使用$or,值为数组,数组中每个元素为json
- 查询名字是hanmh1,或者年龄大于80的人
- > db.hhh.find({$or:[{"name":"hanmh1"},{age:{$gt:80}}]})
- { "_id" : ObjectId("5fcc3f5ee912c7446eb39006"), "name" : "hanmh1", "age" : 99 }
- { "_id" : ObjectId("5fcc3fb1e912c7446eb3900b"), "name" : "hanmh1", "age" : 40 }
(4)MongoDB数据库对正则表达式的支持
使用 // 或者 $regex编写正则表达式
简单使用:
- > db.hhh.find({"name":/^[abc]/}) 或 db.hhh.find({"name":{$regex:'^[abc]'}})
- { "_id" : ObjectId("5fcc4252e912c7446eb3900c"), "name" : "aaaaa", "age" : 40 }
- { "_id" : ObjectId("5fcc4257e912c7446eb3900d"), "name" : "bbbbb", "age" : 40 }
- { "_id" : ObjectId("5fcc425de912c7446eb3900e"), "name" : "c125", "age" : 40 }
(5)limit和skip
方法limit():用于读取指定数量的文档
- db.集合名称.find().limit(NUMBER)
方法skip():用于跳过指定数量的文档
- db.集合名称.find().skip(NUMBER)
同时使用:
- db.集合名称.find().limit(NUMBER1).skip(NUMBER2)
(6)自定义查询
使用$where后面写一个函数,返回满足条件的数据
查询年龄大于20的人
- > db.hhh.find({$where:function(){return this.age>30;}})
查询名字为hanmh的人:
- > db.hhh.find({$where:function(){return this.name=="hanmh1"}})
{ "_id" : ObjectId("5fcc3f5ee912c7446eb39006"), "name" : "hanmh1", "age" : 99 }
{ "_id" : ObjectId("5fcc3fb1e912c7446eb3900b"), "name" : "hanmh1", "age" : 40 }
(7)投影
在查询到的返回结果中,只选择必要的字段:
- db.集合名称.find({},{字段名称:1,...})
- 参数为字段与值,值为1表示显示,值为0表示不显示
- 特殊:对于_id列默认是显示的,如果不显示需要明确设置为0
- db.hhh.find({},{_id:0,name:1,age:1})
(8)排序
方法sort():用于对集合进行排序
db.集合名称.find().sort({字段:1,....})
参数指定为1为升序排列,指定为-1为降序排序
根据年龄降序排序:
- > db.hhh.find().sort({age:-1})
(9)统计个数
方法count()用于统计结果集中文档条数
- db.集合名称.find(条件).count()
- db.集合名称.count({条件})
- 年龄大于20的人数:> db.hhh.find({age:{$gt:20}}).count()
(10)消除重复(去重)
方法distinct()对数据进行去重
db.集合名称.distinct('去重字段',{条件})
对于年龄大于35的人,进行名字去重:
- db.hhh.distinct("name", {"age":{$gte:35}})
- [ "aaaaa", "bbbbb", "c125", "dw112", "hanhhh", "hanmh1", "hanmh4" ]
五、MongoDB数据库的备份与恢复
1、备份语法
mongodbdump -h dbhost -d dbname -o dbdirectory
- -h:服务器地址,也可以指定端口号
- -d:需要备份的数据库的名称
- -o:备份数据存放的位置,此目录中存放着备份出来的数据
- mongodb -h 127.0.0.1:27017 -d hanmh -o C:\Users\Lenovo\Desktop
D:\Program Files\MongoDB\Server\4.4\bin>mongodump -h 127.0.0.1:27017 -d hanmh -o C:\Users\Lenovo\Desktop
2020-12-06T12:33:32.679+0800 writing hanmh.hhh to C:\Users\Lenovo\Desktop\hanmh\hhh.bson
2020-12-06T12:33:32.763+0800 done dumping hanmh.hhh (11 documents)
2、数据的恢复
mongorestore -h dbhost -d dbname --dir dbdirctory
- -h:服务器地址
- -d:需要恢复的数据库实例
- --dir:备份数据所在的位置
- mongorestore -h 127.0.0.1:27017 -d hanmh --dir C:\Users\Lenovo\Desktop\hanmh
D:\Program Files\MongoDB\Server\4.4\bin>mongorestore -h 127.0.0.1:27017 -d hanmh --dir C:\Users\Lenovo\Desktop\hanmh
2020-12-06T12:38:55.692+0800 The --db and --collection flags are deprecated for this use-case; please use --nsInclude instead, i.e. with --nsInclude=${DATABASE}.${COLLECTION}
2020-12-06T12:38:55.719+0800 building a list of collections to restore from C:\Users\Lenovo\Desktop\hanmh dir
2020-12-06T12:38:55.722+0800 reading metadata for hanmh.hhh from C:\Users\Lenovo\Desktop\hanmh\hhh.metadata.json
2020-12-06T12:38:55.906+0800 restoring hanmh.hhh from C:\Users\Lenovo\Desktop\hanmh\hhh.bson
2020-12-06T12:38:55.921+0800 no indexes to restore
2020-12-06T12:38:55.921+0800 finished restoring hanmh.hhh (11 documents, 0 failures)
2020-12-06T12:38:55.925+0800 11 document(s) restored successfully. 0 document(s) failed to restore.