MongoDB
一、认识MongoDB
1.MongoDB简介
由C++编写,基于分布式文件存储的开源数据库系统
旨在为WEB应用提供可扩展的高性能数据存储方案
将数据存储为文档,数据结构由键值对组成,类似于JSON对象
{
name: "zzh",
age:18,
hobby: ['run', 'eat']
}
2.数据模型
mongodb基本概念,文档,集合,数据库
MongoDB | MySQL |
---|---|
数据库(database) | 数据库(database) |
集合(collection) | 表(table) |
文档(document) | 行(row) |
字段/域(field) | 列(column) |
主键,自动将_id字段设置为主键 | 主键(primary key) |
3.官方文档
https://docs.mongodb.com/manual/
二、库,集合操作
1.连接服务端
-
进入:
mongo
为了上手方便,默认没有用户名和密码
-
退出:
exit
2.库级操作
-
显示所有的库:
show dbs
系统库的介绍
- admin:root数据库,管理用户
- local:本地数据库,永远不会被复制,用来存储仅限本台服务器的数据
- config:用于分片设置
-
切换/创建数据:
use db_name
不存在则创建,插入数据才显示(真正创建),默认进入test数据库
-
查看当前数据库:
db
-
删除当前数据库:
db.dropDatabase()
必须进入该数据库才能删除
3.集合操作(collection)
-
显示当前数据库的所有集合:
show collections
-
创建集合:
db.createCollection('name')
切换到当前数据库运行,
db.createCollection('student')
-
删除集合:
db.collection.drop()
# collection 是集合名称db.student.drop()
注意:在MongDB中,集合只有在内容插入之后才会创建
注意:命令大小写敏感
三、文档(数据)操作
每条数据,就是一个document,就是一条json
1.添加文档
-
添加单条:
db.collection.insert(document)
案例:
db.student.insert({ name:'zzh', age:18, sex:'男', course:['python', 'english', 'java'] })
-
添加多条:
db.collection.insertMany([doc1,doc2,...])
案例:
db.student.insertMany([ {name:'zzh'}, {name:'dx', high: 170}, {name:'pyq', age: 18}, ])
2.查询
语法:db.collection.find(query, porjection)
query:可选,使用查询操作符指定查询条件
porjection:可选,指定返回的键,省略代表返回所有的键
-
查看集合中全部数据:
db.collection.find()
-
格式化显示:
db.collection.find().pretty()
-
查看满足条件的数据:
查询student集合中name=‘zzh’的文档
db.student.find({name:'zzh'})
-
projection 参数的用法
若不指定,默认返回所有的键
-
inclusion(包含)模式
db.collection.find(query, {filed1:1, field2:1})
指定返回 field1,field2,没有不报错不显示案例:
db.student.find({name:'zzh'},{name:1,age:1})
-
exclusion(排除)模式
db.collection.find(query, {filed1:0, field2:0})
排除指定的field1,field2案例:
db.student.find({name:'zzh'},{_id:0})
-
总结:
-
_ id主键,默认返回,需要主动指定_id:0 才会隐藏
-
两种模式不能混用(除了_id字段)
db.student.find({name:'张三'},{name:1, age:0})
错误的用法
3.条件
-
比较条件
-
大于
$gt
:db.collection.find({field: {$gt: value}})
案例:查询年龄大于18的学生数据
db.student.find({age: {$gt: 18}, sex: '男'})
-
大于等于
$gte
-
小于
$lt
-
小于等于
$lte
-
不等于
$ne
-
-
逻辑操作符
条件和条件直接的关系
-
$and
:db.collection.find({$and:[condition1, codition2,...]})
查询年龄大于18,性别为男的学生信息
db.student.find({ $and:[ {age: {$gt:18}}, {sex: '男'} ] })
-
$or
:db.collection.find({$or:[condition1, codition2,...]})
查询年龄大于18,或者性别为男的学生信息
db.student.find({ $or:[ {age: {$gt:18}}, {sex: '男'} ] })
-
4.更新
语法:
db.collection.update(
<query>,
<update>,
{
upsert: <boolean>,
multi:<boolean>,
}
)
参数说明:
- query:查询条件
- update:update的对象和一些更新操作符
- upsert:可选,当设置为true时,如果不存在则插入,默认是false,不插入
- multi:可选,默认false,只更新找到的数据的第一条,如果设置为true,所有结果全部更新
1.全文档替换:db.collection.update(query, document, ...)
案例:db.student.update({sex:'男'},{xxx: 'xxxxx'})
# 这条命令,会将查到的第一条数据,更新为 {xxx: 'xxxxx'}
2.指定修改:db.collection.update(query, {$set:{field1:value, field2:value}}, ....)
案例:
db.student.update(
{name: 'dx'},
{$set:{length:180}},
)
5.删除
语法:
db.collection.remove(
<query>,
{
justOne: <boolean>,
}
)
参数说明:
- query: 查询条件
- justOne:可选,设置为true或者1,只删除查询结果的第一条数据,默认false则删除匹配到的所有数据
删除集合中的所有文档: db.collection.remove({})
案例:删除第一条文档
db.student.remove(
{},
{justOne: true}
)
6.其他操作
(1).排序
db.collection.find().sort({field:1/-1})
使用sort方法对数据进行排序,sort方法可以通过参数指定排序字段,1,升序,-1,降序
案例:学生按照年龄升序排列
db.student.find().sort({age:1})
当然可以选取多字段
(2).limit与skip(可综合使用)
-
limit:选取数据个数
db.collection.find().limit(num)
-
skip:跳过数据个数
db.collection.find().skip(num)
(3).分组与聚合
MongoDB中聚合的方法是aggregate()
- 求和
$sum
案例:统计男女分别多少人
db.student.aggregate([
{$group: {_id:'$sex', num: {$sum:1}}}
])
=> select sex, sum(id) as num from student group by sex;
案例:统计总人数
db.student.aggregate([
{$group:{_id:null, num:{$sum:1}}}
])
=> select count(id) as num from student;
- 求平均值
$avg
案例:统计学生平均年龄
db.student.aggregate([
{$group:{_id:null, avg_age:{$avg:'$age'}}}
])
=> select avg(age) as avg_age from student;
案例:统计男生的平均年龄
db.student.aggregate([
{$match: {sex:'男'}},
{$group:{_id:null, avg_age:{$avg:'$age'}}}
])
=> select avg(age) as avg_age from student where sex='男';
- 最大值
$max
案例:学生中的最大年龄
db.student.aggregate([
{$group: {_id:null, max_age:{$max: '$age'}}}
])
=> select max(age) as max_age from student;
- 最小值
$min