Mongodb的CRUD操作
目录
一、插入文档
1、插入单个文档
2、插入多个文档二、查询文档
1、查询某个集合所有文档
2、根据条件查询三、更新文档
1、更新单个文档
2、更新多个文档四、删除文档
1、删除所有文档
2、删除所有符合条件的文档
3、仅删除一个符合条件的文档
4、删除集合中某个字段 Mongodb字段说明表
Hello,大家好之前文章发的是 Mongodb 部署相关的,今天来说说Mongodb的CRUD操作
首先,大家在学习Mongodb的时候建议先去看官方文档学习,Mongodb有中文文档,中文社区文档跟官方的英文文档中文社区:MongoDB中文社区 (mongoing.com)
中文官方文档:MongoDB中文手册|官方文档中文版 - MongoDB-CN-Manual (mongoing.com)
英文官方文档:What is MongoDB? — MongoDB Manual
内容来源:csdn.net作者昵称:JJW1002
一、插入文档
1、插入单个文档
插入单个文档语法 db.collection.insertOne()
-
db.collection.insertOne(
-
<document>, //要插入到集合中的文档。
-
{
-
writeConcern: <document> //自选。表示写入关注点的文档。省略以使用默认的写入关注点
5 }
6 )
该语法来自与官方文档,writeConcern是插入时配置写入关注点,一般实际操作中我们使用默认的就行语法:
db.collection.insertOne({<field1>:<value1>,<field11>:<value11>...})
Mongodb在插入时候可以不用指定ID,因为Mongodb插入会自动生成uuid,字段名为_id
内容来源:csdn.net作者昵称:JJW1002
原文链接:blog.csdn.net/JJW1002/art…
插入示例
-
>use test //使用test数据库
-
switched to db test
-
> db.user.insertOne( //给user集合插一条文档,如果test数据库没有user集合,插入时会自动创建
-
{
-
"username":"zhangsan",
-
"name":"张三",
-
"age":18
8 });
9 {
-
"acknowledged" : true,
-
"insertedId" : ObjectId("65ed512f1ee2b0cc2d89da65")
12 }
13 >
内容来源:csdn.net作者昵称:JJW1002
原文链接:blog.csdn.net/JJW1002/art…
Mongodb是文档型数据库,我们在插入时数据是json格式的,而且Mongodb插入集合时字段是自定义的,比如我们想在user集合中添加一个(status)状态字段,那我们直接在插入数据的时候制定字段就行,示例如下
[TABLE]
5 }
6 >
-
//我们在查询一下发现staus字段已经有了
-
> db.user.find({"username":"lisi"});
-
{ "_id" : ObjectId("65ed52fd1ee2b0cc2d89da66"), "username" : "lisi", "name" : "李四", "age" : 18, "status" : 0 }
-
>
2、插入多个文档
插入多个文档语法db.collection.insertMany()
-
db.collection.insertMany(
-
[ <document 1> , <document 2>, ... ], //要插入到集合中的文档数组。
-
{
-
writeConcern: <document>, //自选。表示写入关注点的文档。省略以使用默认的写入关注点。
-
ordered: <boolean> //自选。一个布尔值,指定 mongod 实例应执行有序插入还是无序插入。默认为 true 。
6 }
7 )
语法:
db.collection.insertMany([
{<field1>:<value1>,<field11>:<value11>...},
{<field2>:<value2>,<field22>:<value22>..},
{<field3>:<value3>,<field33>:<value33>..}
...
内容来源:csdn.net作者昵称:JJW1002
原文链接:blog.csdn.net/JJW1002/art…
])
示例:
- > db.user.insertMany([
2 {"username":"aa","name":"aa","age":19},
3 {"username":"bb","name":"bb","age":20},
4 {"username":"cc","name":"cc","age":21}
5 ]);
6 {
-
"acknowledged" : true,
-
"insertedIds" : [
9 ObjectId("65ed5814b9c5a893e2db6102"),
10 ObjectId("65ed5814b9c5a893e2db6103"),
11 ObjectId("65ed5814b9c5a893e2db6104")
- ]
13 }
14 >
插入成功我们就可以在Mongodb自带的管理工具MongoDBCompass查看是否成功插入数据
内容来源:csdn.net作者昵称:JJW1002
二、查询文档
查询语法db.collection.find()
1、查询某个集合所有文档
内容来源:csdn.net作者昵称:JJW1002
原文链接:blog.csdn.net/JJW1002/art…
查询所有文档,例如查询user集合所有文档: db.user.find()
-
> db.user.find()
-
{ "_id" : ObjectId("65ed512f1ee2b0cc2d89da65"), "username" : "zhangsan", "name" : "张三", "age" : 18 }
-
{ "_id" : ObjectId("65ed52fd1ee2b0cc2d89da66"), "username" : "lisi", "name" : "李四", "age" : 18, "status" : 0 }
[TABLE]
2、根据条件查询
根据字段内容匹配查询
语法:
db.collection.find({<field>:<value>})
示例:根据username为lisi的查询
-
> db.user.find({"username":"lisi"});
-
{ "_id" : ObjectId("65ed52fd1ee2b0cc2d89da66"), "username" : "lisi", "name" : "李四", "age" : 18, "status" : 0 }
-
>
正则匹配查询
语法: db.collection.find([<field>:/<value>/])双斜杠中间为正则或者模糊匹配查询
示例:
内容来源:csdn.net作者昵称:JJW1002
-
1、匹配name中有"张"的文档
-
db.user.find({"name":/张/});
-
{ "_id" : ObjectId("65ed512f1ee2b0cc2d89da65"), "username" : "zhangsan", "name" : "张三", "age" : 18 }
-
{ "_id" : ObjectId("65ed5dddb9c5a893e2db6105"), "username" : "zhanger", "name" : "张二", "age" : 18 }
-
{ "_id" : ObjectId("65ed5debb9c5a893e2db6106"), "username" : "zhangyi", "name" : "张一", "age" : 18 }
-
>
-
2、匹配name中以"李"开头的文档
-
> db.user.find({"name":/^李/});
-
{ "_id" : ObjectId("65ed52fd1ee2b0cc2d89da66"), "username" : "lisi", "name" : "李四", "age" : 18, "status" : 0 }
-
{ "_id" : ObjectId("65ed5e5bb9c5a893e2db6107"), "username" : "lisice", "name" : "李四测", "age" : 18 }
-
>
12
-
3、匹配name中以"测"结尾的文档
-
> db.user.find({"name":/测$/});
-
{ "_id" : ObjectId("65ed5e5bb9c5a893e2db6107"), "username" : "lisice", "name" : "李四测", "age" : 18 }
-
>
17
18
-
4\正则表达式匹配密码
-
db.user.find({"plainText": { $regex: /^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[a-zA-Z].*[a-zA-Z].*[a-zA-Z].*[a-zA-Z])(?=.*?[\d].*[\d]).{8,}$/ } }
时间范围查询
语法:
db.collection.find({<field>:{'$gte':new Date('2022-03-12'),'$lte':new Date('2022-04-14')}}) (>) 大于 - $gt
(<) 小于 - $lt
(>=) 大于等于 - $gte (<= ) 小于等于 - $lte
内容来源:csdn.net作者昵称:JJW1002
原文链接:blog.csdn.net/JJW1002/art…
示例:
1 > db.user.find({"createTime":{'$gte':new Date('2023-03-12'),'$lte':new Date('2024-04-14')}});
-
{ "_id" : ObjectId("65ed512f1ee2b0cc2d89da65"), "username" : "zhangsan", "name" : "张三", "age" : 18, "createTime" : ISODate("2024-03-10
-
>
查询返回指定字段语法:
db.collection.find([<field>:/<value>/],{<field>:1,<filed2>:1. })
<filed1>:1表示返回指定的字段名称,1表示返回,0表示不返回
如果不指定_id默认返回,想要_id不返回需要指定{_id:0}
示例:
-
//指定查询返回username,age
-
> db.user.find({"username":"zhangsan"},{username:1,age:1});
-
{ "_id" : ObjectId("65ed512f1ee2b0cc2d89da65"), "username" : "zhangsan", "age" : 18 }
-
>
5
-
//指定不返回_id
-
> db.user.find({"username":"zhangsan"},{_id:0,username:1,age:1});
-
{ "username" : "zhangsan", "age" : 18 }
-
>
三、更新文档
1、更新单个文档
db.collection.updateOne() 查找与筛选器匹配的单个文档,并应用指定的更新修改
内容来源:csdn.net作者昵称:JJW1002
原文链接:blog.csdn.net/JJW1002/art…
语法:
db.collection.updateOne({<field> : <value>},{$set:{<field1> : <value1>,<field2> :<value2>...}})
示例:
[TABLE]
更新后数据
2、更新多个文档
db.collection.updateMany()查找与筛选器匹配的单个文档,并应用指定的更新修改
语法:
db.collection.updateMany(
{<field> : <value>},
内容来源:csdn.net作者昵称:JJW1002
原文链接:blog.csdn.net/JJW1002/art…
{
$set:{<field1> : <value1>,<field2> :<value2>...}
})
示例:
-
//我们来更新age大于18的数据,age改为25,status改为1
-
先查询一下age大于18的数据有那些
-
> db.user.find({"age":{$gt:18}})
-
{ "_id" : ObjectId("65ed512f1ee2b0cc2d89da65"), "username" : "zhangsan", "name" : "张三", "age" : 20, "createTime" : ISODate("2024-03-10
[TABLE]
-
> db.user.updateMany( { "age": { $gt: 18 }}, { $set: { "age": 25, "status": 1 } } );
-
{ "acknowledged" : true, "matchedCount" : 4, "modifiedCount" : 3 }
-
>
-
//执行之后显示成功4条,我们在查询一下
16
17
-
> db.user.find({"age":{$gt:18}})
-
{ "_id" : ObjectId("65ed512f1ee2b0cc2d89da65"), "username" : "zhangsan", "name" : "张三", "age" : 25, "createTime" : ISODate("2024-03-10
[TABLE]
内容来源:csdn.net作者昵称:JJW1002
四、删除文档
1、删除所有 文档
db.collection.deleteMany({})
2、删除所有符合条件的文档
语法:
db.collection.deleteMany({ <field1>: <value1>, ... })
示例:
[TABLE]
3、仅删除一个符合条件的文档
db.collection.deleteOne( { <field1>: <value1>, ... )
内容来源:csdn.net作者昵称:JJW1002
原文链接:blog.csdn.net/JJW1002/art…
示例:
-
//删除username为cc的数据
-
> db.user.deleteOne({"username":"cc"});
-
{ "acknowledged" : true, "deletedCount" : 1 }
-
>
-
> db.user.find(); ;
-
{ "_id" : ObjectId("65ed512f1ee2b0cc2d89da65"), "username" : "zhangsan", "name" : "张三", "age" : 25, "createTime" : ISODate("2024-03-10
-
{ "_id" : ObjectId("65ed52fd1ee2b0cc2d89da66"), "username" : "lisi", "name" : "李四", "age" : 18, "status" : 0 }
-
{ "_id" : ObjectId("65ed5814b9c5a893e2db6103"), "username" : "bb", "name" : "bb", "age" : 25, "status" : 1 }
-
{ "_id" : ObjectId("65ed5dddb9c5a893e2db6105"), "username" : "zhanger", "name" : "张二", "age" : 18 }
-
{ "_id" : ObjectId("65ed5debb9c5a893e2db6106"), "username" : "zhangyi", "name" : "张一", "age" : 18 }
-
{ "_id" : ObjectId("65ed5e5bb9c5a893e2db6107"), "username" : "lisice", "name" : "李四测", "age" : 18 }
-
>
13
14
-
//在匹配删除name中有"李"的文档,其实里面有两条数据,但是执行之后只删除了一条
-
//因为deleteOne只删除一条文档
-
> db.user.deleteOne({"name":/李/});
-
{ "acknowledged" : true, "deletedCount" : 1 }
-
>
-
> db.user.find();
-
{ "_id" : ObjectId("65ed512f1ee2b0cc2d89da65"), "username" : "zhangsan", "name" : "张三", "age" : 25, "createTime" : ISODate("2024-03-10
-
{ "_id" : ObjectId("65ed5814b9c5a893e2db6103"), "username" : "bb", "name" : "bb", "age" : 25, "status" : 1 }
-
{ "_id" : ObjectId("65ed5dddb9c5a893e2db6105"), "username" : "zhanger", "name" : "张二", "age" : 18 }
-
{ "_id" : ObjectId("65ed5debb9c5a893e2db6106"), "username" : "zhangyi", "name" : "张一", "age" : 18 }
-
{ "_id" : ObjectId("65ed5e5bb9c5a893e2db6107"), "username" : "lisice", "name" : "李四测", "age" : 18 }
-
>
4、删除集合中某个字段
内容来源:csdn.net作者昵称:JJW1002
原文链接:blog.csdn.net/JJW1002/art…
删除<field>字段,$exists:true存在,<field>:null为空则删除
语法:
db.collection.update({ <field>: { "$exists": true } }, { "$unset": { <field>:null } }, { multi: true });
示例:数据库中有aa字段
-
//删除aa字段
-
db.user.update({
3 "aa": {
4 "$exists": true
5 }
6 }, {
-
"$unset": {
-
"aa":null
9 }
-
}, {
-
multi: true
12 });
-
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
-
//执行之后删除成功 1
-
>
-
> db.user.find()
内容来源:csdn.net作者昵称:JJW1002
-
{ "_id" : ObjectId("65ed512f1ee2b0cc2d89da65"), "username" : "zhangsan", "name" : "张三", "age"作:者主25页,:"htctprse://abtloeg.Tcisdmne.n"et/:JJWI1S0O0D2ate("2024-03-10
-
{ "_id" : ObjectId("65ed5814b9c5a893e2db6103"), "username" : "bb", "name" : "bb", "age" : 25, "status" : 1 }19
{ "_id" : ObjectId("65ed5dddb9c5a893e2db6105"), "username" : "zhanger", "name" : "张二", "age" : 18 }20
{ "_id" : ObjectId("65ed5debb9c5a893e2db6106"), "username" : "zhangyi", "name" : "张一", "age" : 18 }21
{ "_id" : ObjectId("65ed5e5bb9c5a893e2db6107"), "username" : "lisice", "name" : "李四测", "age" : 18 }22 >
Mongodb字段说明表
内容来源:csdn.net作者昵称:JJW1002
原文链接:blog.csdn.net/JJW1002/art…
内容来源:csdn.net作者昵称:JJW1002
以上就是Mongodb的基本CRUD操作,如果大家还想学习更多的Mongodb操作请参考官方文档进行学习,我这里也有一个原M文o链n接g:ohDttBps权://b威log指.cs南dn.中net文/JJ版W1**.0p0d2f**/a,rtic如le/有det人ails想/13要66的012呢25
可以私信我,免费发给大家一起学习,

文章知识点与官方知识档案匹配,可进一步学习相关知识
MySQL入门技能树 数据库组成 表 84568 人正在系统学习中
内容来源:csdn.net作者昵称:JJW1002
Hello,大家好之前文章发的是