MongoDB的CRUD操作(入门)
MongoDB的简单介绍:
1:MongoDB是什么?
- mongodb是非关系数据库 但是是非关系数据库当中功能最丰富,最像关系数据库的
- MongoDB是一个基于分布式文件存储的数据库。
- 由C++语言编写。旨在为WEB应用提供可扩展的高性能数据存储解决方案。
2:MongoDB的三大特点:
- 高性能
- 易部署
- 易使用
- 存储数据非常方便
-
文件存储格式为BSON(一种JSON的扩展)。
3:先来介绍我们的插入语句吧
1:插入语句:
插入一条数据:
> db.jie.insert({name:"jie",age:18})
WriteResult({ "nInserted" : 1 })
可以使用:db.集合名.find()查看
> db.jie.find() { "_id" : ObjectId("6069ac8300fc7b062bb3ed94"), "name" : "jie", "age" : 18 }
这里id可以自己设置
可以指定id 不过最好不要指定
插入多条:
> db.jie.insert([{name:"luyunjie",age:18},{name:"jiebao",age:19}])
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 2,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})
mongodb也可以使用js语法 用for循环插入多条数据:
> for (var i=1;i<=10;i++){ db.jie1.insert({name: "jie"+i,age: i}) } WriteResult({ "nInserted" : 1 })
这里不为10是因为for循环是一条一条插入
然后只显示最后一条插入的数量 单用find()查看还是可以查看10条数据
> db.jie1.find()
{ "_id" : ObjectId("6069b34700fc7b062bb3ed97"), "name" : "jie1", "age" : 1 }
{ "_id" : ObjectId("6069b34700fc7b062bb3ed98"), "name" : "jie2", "age" : 2 }
{ "_id" : ObjectId("6069b34700fc7b062bb3ed99"), "name" : "jie3", "age" : 3 }
{ "_id" : ObjectId("6069b34700fc7b062bb3ed9a"), "name" : "jie4", "age" : 4 }
{ "_id" : ObjectId("6069b34700fc7b062bb3ed9b"), "name" : "jie5", "age" : 5 }
{ "_id" : ObjectId("6069b34700fc7b062bb3ed9c"), "name" : "jie6", "age" : 6 }
{ "_id" : ObjectId("6069b34700fc7b062bb3ed9d"), "name" : "jie7", "age" : 7 }
{ "_id" : ObjectId("6069b34700fc7b062bb3ed9e"), "name" : "jie8", "age" : 8 }
{ "_id" : ObjectId("6069b34700fc7b062bb3ed9f"), "name" : "jie9", "age" : 9 }
{ "_id" : ObjectId("6069b34700fc7b062bb3eda0"), "name" : "jie10", "age" : 10 }
2:查询语句
基本的运算符介绍:
出几个题目 配合查询语句来一一实现:
1:只查询字段name的值:
> db.jie1.find({},{name:1}) { "_id" : ObjectId("6069b34700fc7b062bb3ed97"), "name" : "jie1" } { "_id" : ObjectId("6069b34700fc7b062bb3ed98"), "name" : "jie2" } { "_id" : ObjectId("6069b34700fc7b062bb3ed99"), "name" : "jie3" } { "_id" : ObjectId("6069b34700fc7b062bb3ed9a"), "name" : "jie4" } { "_id" : ObjectId("6069b34700fc7b062bb3ed9b"), "name" : "jie5" } { "_id" : ObjectId("6069b34700fc7b062bb3ed9c"), "name" : "jie6" } { "_id" : ObjectId("6069b34700fc7b062bb3ed9d"), "name" : "jie7" } { "_id" : ObjectId("6069b34700fc7b062bb3ed9e"), "name" : "jie8" } { "_id" : ObjectId("6069b34700fc7b062bb3ed9f"), "name" : "jie9" } { "_id" : ObjectId("6069b34700fc7b062bb3eda0"), "name" : "jie10" }
无论查询什么 id都会一直在的
2:查询age大于5 小于8的所有数据:
> db.jie1.find({age:{$gte:5,$lte:8}}) { "_id" : ObjectId("6069b34700fc7b062bb3ed9b"), "name" : "jie5", "age" : 5 } { "_id" : ObjectId("6069b34700fc7b062bb3ed9c"), "name" : "jie6", "age" : 6 } { "_id" : ObjectId("6069b34700fc7b062bb3ed9d"), "name" : "jie7", "age" : 7 } { "_id" : ObjectId("6069b34700fc7b062bb3ed9e"), "name" : "jie8", "age" : 8 }
运用我们的运算符来达到效果 运算符的语法是
db.集合名.find(
键:{运算符:值}
)
3:查询age为1,4, 5, 9 的所有数据:
> db.jie1.find({age:{$in:[1,4,5,9]}}) { "_id" : ObjectId("6069b34700fc7b062bb3ed97"), "name" : "jie1", "age" : 1 } { "_id" : ObjectId("6069b34700fc7b062bb3ed9a"), "name" : "jie4", "age" : 4 } { "_id" : ObjectId("6069b34700fc7b062bb3ed9b"), "name" : "jie5", "age" : 5 } { "_id" : ObjectId("6069b34700fc7b062bb3ed9f"), "name" : "jie9", "age" : 9 }
运用运算符达到效果
4:把age倒序排列(运用sort()来排序):
> db.jie1.find().sort({age:-1})
{ "_id" : ObjectId("606a79d6945dd5f6933d5845"), "name" : "jie10", "age" : 10 }
{ "_id" : ObjectId("606a79d6945dd5f6933d5844"), "name" : "jie9", "age" : 9 }
{ "_id" : ObjectId("606a79d6945dd5f6933d5843"), "name" : "jie8", "age" : 8 }
{ "_id" : ObjectId("606a79d6945dd5f6933d5842"), "name" : "jie7", "age" : 7 }
{ "_id" : ObjectId("606a79d6945dd5f6933d5841"), "name" : "jie6", "age" : 6 }
{ "_id" : ObjectId("606a79d6945dd5f6933d5840"), "name" : "jie5", "age" : 5 }
{ "_id" : ObjectId("606a79d6945dd5f6933d583f"), "name" : "jie4", "age" : 4 }
{ "_id" : ObjectId("606a79d6945dd5f6933d583e"), "name" : "jie3", "age" : 3 }
{ "_id" : ObjectId("606a79d6945dd5f6933d583d"), "name" : "jie2", "age" : 2 }
{ "_id" : ObjectId("606a79d6945dd5f6933d583c"), "name" : "jie1", "age" : 1 }
运用sort()来进行倒序
语法:db.集合名.find().sort({查询的字段:-1 })
-1为倒序 1为正序
聚合函数:
简单介绍一下聚合函数的管道:
表达式 | 描述 | 实例 |
---|---|---|
$sum | 计算总和。 | db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$sum : "$likes"}}}]) |
$avg | 计算平均值 | db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$avg : "$likes"}}}]) |
$min | 获取集合中所有文档对应值得最小值。 | db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$min : "$likes"}}}]) |
$max | 获取集合中所有文档对应值得最大值。 | db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$max : "$likes"}}}]) |
$push | 在结果文档中插入值到一个数组中。 | db.mycol.aggregate([{$group : {_id : "$by_user", url : {$push: "$url"}}}]) |
$addToSet | 在结果文档中插入值到一个数组中,但不创建副本。 | db.mycol.aggregate([{$group : {_id : "$by_user", url : {$addToSet : "$url"}}}]) |
$first | 根据资源文档的排序获取第一个文档数据。 | db.mycol.aggregate([{$group : {_id : "$by_user", first_url : {$first : "$url"}}}]) |
$last | 根据资源文档的排序获取最后一个文档数据 | db.mycol.aggregate([{$group : {_id : "$by_user", last_url : {$last : "$url"}}}]) |
聚合函数语法:
db.集合名.aggregate([
{管道:{ 表达式}
....
]})
题目自测:
1:统计男生,女生的总年龄:
准备数据:
db.xiaojie3.insert([
{name:"小杰",sex:1,age:18},
{name:"杰宝",sex:1,age:19},
{name:"小红",sex:2,age:20},
{name:"小青",sex:2,age:16}
])
代码:
db.xiaojie3.aggregate([
{$group:{
_id:"$sex",
num:{$sum:"$age"}
}
}])
分页查询(skip和limit):
语法:
db.集合名.find().skip(跳过多少条的数字).limit(查询多少条的数字)
skip是可选值 可不添加
练习题目:
1:只查询age为10和9的数据:
> db.jie1.find().sort({age:-1}).limit(2)
{ "_id" : ObjectId("606a79d6945dd5f6933d5845"), "name" : "jie10", "age" : 10 }
{ "_id" : ObjectId("606a79d6945dd5f6933d5844"), "name" : "jie9", "age" : 9 }
2:降序跳过4条数据 查4条:
> db.jie1.find().sort({age:-1}).skip(4).limit(4)
{ "_id" : ObjectId("606a79d6945dd5f6933d5841"), "name" : "jie6", "age" : 6 }
{ "_id" : ObjectId("606a79d6945dd5f6933d5840"), "name" : "jie5", "age" : 5 }
{ "_id" : ObjectId("606a79d6945dd5f6933d583f"), "name" : "jie4", "age" : 4 }
{ "_id" : ObjectId("606a79d6945dd5f6933d583e"), "name" : "jie3", "age" : 3
3:修改(更新)语句:
修改器的基本介绍:
如果不用修改器来修改数据 则是替换:
列子:
> db.jie.update({name:"jie"},{name:"xiaojie"}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.jie.find() { "_id" : ObjectId("6069ac8300fc7b062bb3ed94"), "name" : "xiaojie" } { "_id" : ObjectId("6069ad8700fc7b062bb3ed95"), "name" : "luyunjie", "age" : 18 } { "_id" : ObjectId("6069ad8700fc7b062bb3ed96"), "name" : "jiebao", "age" : 19 } >
到达的是替换的效果
我们继续举列子来讲解:
1:修改name为luyunjie的字段 把luyunjie改为xiaojiejie其他不变:
> db.jie.update({name:"luyunjie"},{$set:{name:"xiaojiejie"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.jie.find()
{ "_id" : ObjectId("6069ac8300fc7b062bb3ed94"), "name" : "xiaojie" }
{ "_id" : ObjectId("6069ad8700fc7b062bb3ed95"), "name" : "xiaojiejie", "age" : 18 }
{ "_id" : ObjectId("6069ad8700fc7b062bb3ed96"), "name" : "jiebao", "age" : 19 }
修改成功 运用修改器
修改器语法:
db.集合名.updata(
{修改的值},
{修改器:{修改的值}
})
2:给age为20的数据递加或递减5:
递增:
> db.jie.update({age:20},{$inc:{age:5}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.jie.find()
{ "_id" : ObjectId("6069ac8300fc7b062bb3ed94"), "name" : "xiaojie" }
{ "_id" : ObjectId("6069ad8700fc7b062bb3ed95"), "name" : "xiaojiejie", "age" : 25 }
{ "_id" : ObjectId("6069ad8700fc7b062bb3ed96"), "name" : "jiebao", "age" : 19 }
递减:
> db.jie.update({age:18},{$inc:{age:-5}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.jie.find()
{ "_id" : ObjectId("6069ac8300fc7b062bb3ed94"), "name" : "xiaojie" }
{ "_id" : ObjectId("6069ad8700fc7b062bb3ed95"), "name" : "xiaojiejie", "age" : 20 }
{ "_id" : ObjectId("6069ad8700fc7b062bb3ed96"), "name" : "jiebao", "age" : 19 }
>
3:练习为修改器为修改多个值
题目:把name里面的数据改为小杰,age数据递增20,sex字段删除,dta字段改为bianhao;
准备数据:
> db.xiaojie.insert({name:"jie",age:20,sex:"boy",dta:"123"})
WriteResult({ "nInserted" : 1 })
> db.xiaojie.find()
{ "_id" : ObjectId("6069c59800fc7b062bb3eda1"), "name" : "jie", "age" : 20, "sex" : "boy", "dta" : "123" }
题目答案:
> db.xiaojie.update({name:"jie"},{$set:{name:"小杰"},$inc:{age:20},$rename:{dta:"bianhao"},$unset:{sex:true}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.xiaojie.find()
{ "_id" : ObjectId("6069c59800fc7b062bb3eda1"), "name" : "小杰", "age" : 40, "bianhao" : "123" }
这里有几个点 多条语句的写法如上 db.集合名.update
({修改字段:字段值}, {
修改器:{},
修改器:{},
修改器:{},
修改器:{}
})
4:删除语句:
删除语法是:
db.集合名.remove({条件},是否删除一条)
1:删除一条数据:
> db.jie1.remove({},true)
WriteResult({ "nRemoved" : 1 })
> db.jie1.find()
{ "_id" : ObjectId("6069b34700fc7b062bb3ed98"), "name" : "jie2", "age" : 2 }
{ "_id" : ObjectId("6069b34700fc7b062bb3ed99"), "name" : "jie3", "age" : 3 }
{ "_id" : ObjectId("6069b34700fc7b062bb3ed9a"), "name" : "jie4", "age" : 4 }
{ "_id" : ObjectId("6069b34700fc7b062bb3ed9b"), "name" : "jie5", "age" : 5 }
{ "_id" : ObjectId("6069b34700fc7b062bb3ed9c"), "name" : "jie6", "age" : 6 }
{ "_id" : ObjectId("6069b34700fc7b062bb3ed9d"), "name" : "jie7", "age" : 7 }
{ "_id" : ObjectId("6069b34700fc7b062bb3ed9e"), "name" : "jie8", "age" : 8 }
{ "_id" : ObjectId("6069b34700fc7b062bb3ed9f"), "name" : "jie9", "age" : 9 }
{ "_id" : ObjectId("6069b34700fc7b062bb3eda0"), "name" : "jie10", "age" : 10 }
2:删除全部数据:
> db.jie1.remove({})
WriteResult({ "nRemoved" : 9 })
简单介绍了一下MongoDB的CRUD操作 这篇博客会慢慢完善成MongoDB笔记 加油一起努力!