目录
一、基本命令:
- 查看当前数据库:
db
- 创建并进入数据库:
use jxx
- 查看所有数据库:
show dbs //(空库不显示)
- 删除当前数据库,需要先进入当前数据库:
db.dropDatabase()
- 创建集合(集合与表的区别:集合无序、不可重复,列表有序、可重复):
db.createCollection('cs')
- 查看集合:
show collections
- 插入文档&&隐式创建集合的一种方式:
db.cs.insert({'name':'swk','age':18})
- 删除集合:
db.cs.drop()
- 多文档插入:
db.cs.insertMany([{'name':'ts','age':18},{'name':'zbj','age':18},{'name':'shs','age':18}])
- 查找(查看集合内容):
db.cs.find()
- 写入多条数据:
db.cs.insertMany([{ }])
- 修改一个数据:
db.cs.update({'id':1},{$set:{'id':4}})
- 更新 一条:( 第一个是原本数据,第二个是要修改为的内容)
db.comment.update({"content":"喝水是生命体通过口腔摄入水分的方式,人体每天通过口腔摄入的液体大约有2升"},{$set:{"content":"喝水增加了尿量,能使有害物质及时排出体内"}})
- 更新多条:
db.comment.update({"content":"喝水是生命体通过口腔摄入水分的方式,人体每天通过口腔摄入的液体大约有2升"},{$set:{"content":"喝水增加了尿量,能使有害物质及时排出体内"}},false,true)
注:第3个参数表示如果不存在,是否新创建,第4个参数表示是否更新多条数据。
- 删除:
db.comment.remove({"nickname":"爱德华"})
- 删除全部:
db.comment.remove({})
二、常用命令:
1、基本查询:
- 易读方式的查询:
db.comment.find().pretty()
- 排序查询-降序:
db.comment.find().sort({userid:-1}).pretty()
- 排序查询-升序:
db.comment.find().sort({userid:1}).pretty()
- 排序查询-多个条件:
db.cjd.find().sort({userid:-1,likenum:1}).pretty()
2、按照条件查询
- 按条件查询--与:
db.cjd.find({$and:[{"userid":"1005", "nickname":"罗密欧"}]}).pretty()
- 按条件查询--或:
db.cjd.find({$or:[{"userid":"1002"},{"userid":"1003"}]}).pretty()
- 按条件查询--大于(greater than):
db.comment.find({"userid":{$gt:"1005"}}).pretty()
- 按条件查询--小于(less than)
db.comment.find({"userid":{$lt:"1004"}}).pretty()
- 按条件查询--大于等于(greater than or equal)
db.comment.find({"userid":{$gte:"1005"}}).pretty()
- 按条件查询--小于等于(less than or equal)
db.comment.find({"userid":{$lte:"1003"}}).pretty()
- 按条件查询--不等于(not equal)
db.comment.find({"userid":{$ne:"1005"}}).pretty()
- 按条件查询--包含(in)
db.comment.find({"_id":{$in:["1","3"]}}).pretty()
- 按条件查询--不包含(not in)
db.comment.find({"_id":{$nin:["1","3","5"]}}).pretty()
3、按特定类型查询
- 使用Null类型查询
db.comment.find({"state":null}).pretty()
- 使用正则表达式查询
db.comment.find({"content":/^专家/}).pretty()
- 使用精确查询
db.comment.find({'name':'zhu'})
db.comment.find({"phone":{"homePhone":"62771541","mobilePhone":"13262984142"}}).pretty()
- 使用点查询
db.comment.find({"phone.homePhone":"82174911"}).pretty()
4、统计查询
- 统计所有
db.comment.count()
- 按照条件统计
db.comment.count({userid:"1003"})
5、分页查询
示例:limit()表示读取指定数量的数据,skip()表示跳过指定数量的数据。
- 读取三条文档
db.comment.find().limit(3)
- 跳过三条后读取
db.comment.find().skip(3)
- 跳过三条后,查询2条
db.comment.find().limit(2).skip(3) //limit是输入要查询的数量,skip是跳过的数量
6、聚合管道操作
- 使用$group将文档进行分组
db.comment.aggregate([{$group:{"_id":"$userid"}}]).pretty()
- 使用$limit指定文档的读取数量
db.comment.aggregate({$limit:3}).pretty()
- 使用$match查询符合匹配条件的数据
db.comment.aggregate([{$match:{"nickname":"罗密欧"}}]).pretty()
- 使用$sort排序
db.comment.aggregate([{$sort:{"age":-1}}]).pretty()
- 使用$project过滤掉指定字段后展示文档,
db.comment.aggregate([{$project:{"_id":0}}]).pretty()
- $skip跳过指定条数后展示文档
db.comment.aggregate({$skip:4}).pretty()
7、分组
- 创建product集合
db.product.insertMany([{"_id":"1","name":"iPhone 8","price":3000,"type":"电子通讯"},{"_id":"2","name":"adidasneo","price":700,"type":"服装"},{"_id":"3","name":"nike air max 90","price":760,"type":"服装"},{"_id":"4","name":"HuaWei mate30","price":5000,"type":"电子通讯"},{"_id":"5","name":"vivo x27","price":2000,"type":"电子通讯"}])
- 按类型type进行分组,并计算各个分组的价格price总和。
db.product.aggregate([{$group:{"_id":"$type","价格":{$sum:"$price"}}}]).pretty()
db.product.aggregate([{$group:{"_id":"$class","随便写":{$sum:"$total"}}}]).pretty()
- 按类型type进行分组,并计算各个分组的价格price平均值
db.product.aggregate([{$group:{"_id":"$type","price":{$avg:"$price"}}}]).pretty()
- 按类型type进行分组,并找出各个分组的价格price最小值
db.product.aggregate([{$group:{"_id":"$type","price":{$avg:"$price"}}}]).pretty()
- 按类型type进行分组,并找出各个分组的价格price最大值
db.product.aggregate([{$group:{"_id":"$type","price":{$max:"$price"}}}]).pretty()
- 按类型type进行分组,并将各个分组的产品$name字段插入到一个数组tags
db.product.aggregate([{$group:{"_id":"$type","tags":{$push:"$name"}}}]).pretty()
- 按类型type进行分组,并获取各个分组中$name字段的第一个产品
db.product.aggregate([{$group:{"_id":"$type","product":{$first:"$name"}}}]).pretty()
- 按类型type进行分组,并获取各个分组中$name字段的最后一个产品
db.product.aggregate([{$group:{"_id":"$type","product":{$last:"$name"}}}]).pretty()
8、索引
- 查看索引
db.comment.getIndexes()
- 创建升序索引
db.comment.createIndex({userid:1})
- 创建复合索引
db.comment.createIndex({userid:1,nickname:-1})
- 移除索引
db.comment.dropIndex({userid:1})
- 查看索引是否生效
db.comment.find({userid:"1003"}).explain()
备注:"stage" : "FETCH" 是基于索引的扫描,"stage" : "COLLSCAN", 表示全集合扫描
9、设置权限用户的步骤--简单
- 1.进入某个数据库比如:
use jxx
- 2.创建用户
db.createUser({user:"Admin",pwd:passwordPrompt(),roles:[{role:"readWrite",db:"jxx"}]})
- 3.服务器登陆。重新启动服务器,打开命令行,采用以下命令进入权限模式
mongod --dbpath C:\mongodb\data\db --logpath C:\mongodb\data\logs\mongo.log --logappend --auth
- 4.用户端登陆。注意必须先进入对应数据库,然后在进行账号登陆。
use jxx
- 5.登陆
db.auth('Admin','123456')
- 6.进行数据的读写
10、设置权限用户的步骤--复杂
- 1.进入某个数据库比如:
use admin
- 2.创建管理员
db.createUser({user:"itcastAdmin",pwd:passwordPrompt(),roles:[{role:"userAdminAnyDatabase",db:"admin"}]})
- 3.创建用户
db.createUser({user:"itcastUser",pwd:passwordPrompt(),roles:[{role:"read",db:"admin"}]})
- 4修改用户权限
db.updateUser("itcastUser",{roles:[{role:"read",db:"admin"},{role:"readAnyDatabase",db:"admin"}]})
- 5.重新启动服务器,打开命令行,采用以下命令进入权限模式
mongod --dbpath C:\mongodb\data\db --logpath C:\mongodb\data\logs\mongo.log --logappend --auth
- 6.注意必须先进入对应数据库,然后在进行账号登陆。
use admin
- 7.登陆
db.auth('itcastUser','123456')
- 8.进行数据的读写