目录
Hello,大家好之前文章发的是Mongodb部署相关的,今天来说说Mongodb的CRUD操作
首先,大家在学习Mongodb的时候建议先去看官方文档学习,Mongodb有中文文档,中文社区文档跟官方的英文文档
中文社区:MongoDB中文社区 (mongoing.com)
中文官方文档:MongoDB中文手册|官方文档中文版 - MongoDB-CN-Manual (mongoing.com)
英文官方文档:What is MongoDB? — MongoDB Manual
一、插入文档
1、插入单个文档
插入单个文档语法 db.collection.insertOne()
db.collection.insertOne(
<document>, //要插入到集合中的文档。
{
writeConcern: <document> //自选。表示写入关注点的文档。省略以使用默认的写入关注点
}
)
该语法来自与官方文档,writeConcern是插入时配置写入关注点,一般实际操作中我们使用默认的就行
语法:
db.collection.insertOne({<field1>:<value1>,<field11>:<value11>...})
Mongodb在插入时候可以不用指定ID,因为Mongodb插入会自动生成uuid,字段名为_id
插入示例
>use test //使用test数据库
switched to db test
> db.user.insertOne( //给user集合插一条文档,如果test数据库没有user集合,插入时会自动创建
{
"username":"zhangsan",
"name":"张三",
"age":18
});
{
"acknowledged" : true,
"insertedId" : ObjectId("65ed512f1ee2b0cc2d89da65")
}
>
Mongodb是文档型数据库,我们在插入时数据是json格式的,而且Mongodb插入集合时字段是自定义的,比如我们想在user集合中添加一个(status)状态字段,那我们直接在插入数据的时候制定字段就行,示例如下
> db.user.insertOne({"username":"lisi","name":"李四","age":18,"status":0});
{
"acknowledged" : true,
"insertedId" : ObjectId("65ed52fd1ee2b0cc2d89da66")
}
>
//我们在查询一下发现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 。
}
)
语法:
db.collection.insertMany([
{<field1>:<value1>,<field11>:<value11>...},
{<field2>:<value2>,<field22>:<value22>..},
{<field3>:<value3>,<field33>:<value33>..}
...
])
示例:
> db.user.insertMany([
{"username":"aa","name":"aa","age":19},
{"username":"bb","name":"bb","age":20},
{"username":"cc","name":"cc","age":21}
]);
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("65ed5814b9c5a893e2db6102"),
ObjectId("65ed5814b9c5a893e2db6103"),
ObjectId("65ed5814b9c5a893e2db6104")
]
}
>
插入成功我们就可以在Mongodb自带的管理工具MongoDBCompass查看是否成功插入数据
二、查询文档
查询语法db.collection.find()
1、查询某个集合所有文档
查询所有文档,例如查询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 }
{ "_id" : ObjectId("65ed5814b9c5a893e2db6102"), "username" : "aa", "name" : "aa", "age" : 19 }
{ "_id" : ObjectId("65ed5814b9c5a893e2db6103"), "username" : "bb", "name" : "bb", "age" : 20 }
{ "_id" : ObjectId("65ed5814b9c5a893e2db6104"), "username" : "cc", "name" : "cc", "age" : 21 }
>
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>/])
双斜杠中间为正则或者模糊匹配查询
示例:
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 }
>
3、匹配name中以"测"结尾的文档
> db.user.find({"name":/测$/});
{ "_id" : ObjectId("65ed5e5bb9c5a893e2db6107"), "username" : "lisice", "name" : "李四测", "age" : 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
示例:
> 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-10T15:28:26Z") }
>
- 查询返回指定字段
语法:
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 }
>
//指定不返回_id
> db.user.find({"username":"zhangsan"},{_id:0,username:1,age:1});
{ "username" : "zhangsan", "age" : 18 }
>
三、更新文档
1、更新单个文档
db.collection.updateOne()
查找与筛选器匹配的单个文档,并应用指定的更新修改
语法:
db.
collection.updateOne({<field> : <value>},{$set:{<field1> : <value1>,<field2> :<value2>...}})
示例:
//根据username修改age,status
> db.user.update(
{ "username": "zhangsan" },
{ $set: { "age": 20, "status": 0 } }
);
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>
更新后数据
2、更新多个文档
db.collection.updateMany()查找与筛选器匹配的单个文档,并应用指定的更新修改
语法:
db.
collection.
updateMany(
{<field> : <value>},
{
$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-10T15:28:26Z"), "status" : 0 }
{ "_id" : ObjectId("65ed5814b9c5a893e2db6102"), "username" : "aa", "name" : "aa", "age" : 19 }
{ "_id" : ObjectId("65ed5814b9c5a893e2db6103"), "username" : "bb", "name" : "bb", "age" : 20 }
{ "_id" : ObjectId("65ed5814b9c5a893e2db6104"), "username" : "cc", "name" : "cc", "age" : 21 }
>
> db.user.updateMany( { "age": { $gt: 18 }}, { $set: { "age": 25, "status": 1 } } );
{ "acknowledged" : true, "matchedCount" : 4, "modifiedCount" : 3 }
>
//执行之后显示成功4条,我们在查询一下
> db.user.find({"age":{$gt:18}})
{ "_id" : ObjectId("65ed512f1ee2b0cc2d89da65"), "username" : "zhangsan", "name" : "张三", "age" : 25, "createTime" : ISODate("2024-03-10T15:28:26Z"), "status" : 1 }
{ "_id" : ObjectId("65ed5814b9c5a893e2db6102"), "username" : "aa", "name" : "aa", "age" : 25, "status" : 1 }
{ "_id" : ObjectId("65ed5814b9c5a893e2db6103"), "username" : "bb", "name" : "bb", "age" : 25, "status" : 1 }
{ "_id" : ObjectId("65ed5814b9c5a893e2db6104"), "username" : "cc", "name" : "cc", "age" : 25, "status" : 1 }
>
四、删除文档
1、删除所有文档
db.collection.deleteMany({})
2、删除所有符合条件的文档
语法:
db.collection.deleteMany({ <field1>: <value1>, ... })
示例:
//删除username为aa的文档
> db.user.deleteMany({"username":"aa"});
{ "acknowledged" : true, "deletedCount" : 1 } //表示执行成功,删除数量1
//删除之后我们在查询发现没有aa那条数据了
> db.user.find();
{ "_id" : ObjectId("65ed512f1ee2b0cc2d89da65"), "username" : "zhangsan", "name" : "张三", "age" : 25, "createTime" : ISODate("2024-03-10T15:28:26Z"), "status" : 1 }
{ "_id" : ObjectId("65ed52fd1ee2b0cc2d89da66"), "username" : "lisi", "name" : "李四", "age" : 18, "status" : 0 }
{ "_id" : ObjectId("65ed5814b9c5a893e2db6103"), "username" : "bb", "name" : "bb", "age" : 25, "status" : 1 }
{ "_id" : ObjectId("65ed5814b9c5a893e2db6104"), "username" : "cc", "name" : "cc", "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 }
>
3、仅删除一个符合条件的文档
db.collection.deleteOne( { <field1>: <value1>, ... )
示例:
//删除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-10T15:28:26Z"), "status" : 1 }
{ "_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 }
>
//在匹配删除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-10T15:28:26Z"), "status" : 1 }
{ "_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、删除集合中某个字段
删除<field>字段,$exists:true存在,<field>:null为空则删除
语法:
db.collection.update({ <field>: { "$exists": true } }, { "$unset": { <field>:null } }, { multi: true });
示例:数据库中有aa字段
//删除aa字段
db.user.update({
"aa": {
"$exists": true
}
}, {
"$unset": {
"aa":null
}
}, {
multi: true
});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
//执行之后删除成功 1
>
> db.user.find()
{ "_id" : ObjectId("65ed512f1ee2b0cc2d89da65"), "username" : "zhangsan", "name" : "张三", "age" : 25, "createTime" : ISODate("2024-03-10T15:28:26Z"), "status" : 1 }
{ "_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 }
>
Mongodb字段说明表
以上就是Mongodb的基本CRUD操作,如果大家还想学习更多的Mongodb操作请参考官方文档进行学习,我这里也有一个MongoDB权威指南中文版.pdf,如有人想要的呢可以私信我,免费发给大家一起学习,
标签:username,name,ObjectId,Mongodb,age,CRUD,db,操作,id From: https://blog.csdn.net/JJW1002/article/details/136601225