【sql】MongoDB 增删改查 高级用法

【sql】MongoDB 增删改查 高级用法

相关使用文档

MongoDB Query API --- MongoDB Manual

https://www.mongodb.com/docs/manual/reference/sql-comparison

复制代码
//增
//新增数据2种方式
db.msg.save({"name":"springboot😀"});
db.msg.insert({"name":"mango good"});
db.msg.save({"name":"springboot",type:"工具书", json:{id:1, name:'张三'}});


//批量新增
db.msg.insertMany(

[
{"name":"springboot1",type:"工具书", user:{id:1, name:'张三'}},
{"name":"springboot2",type:"语文书", user:{id:2, name:'李四'}},
{"name":"springboot3",type:"数学书", user:{id:3, name:'王五'}},
]
);




//循环添加数据
var b = 0
while (b <= 100000) {
//db.msg.save({indexa:b, "name":"springboot",type:"工具书", json:{id:1, name:'张三'}});

db.msg.insertMany(

	[
		{"name":"springboot1",type:"工具书", user:{id:b+1, name:'张三'}},
		{"name":"springboot2",type:"语文书", user:{id:b+2, name:'李四'}},
		{"name":"springboot3",type:"数学书", user:{id:b+3, name:'王五'}},
		{"name":"springboot1",type:"工具书", user:{id:b+4, name:'张三'}},
		{"name":"springboot2",type:"语文书", user:{id:b+5, name:'李四'}},
		{"name":"springboot3",type:"数学书", user:{id:b+6, name:'王五'}},
		{"name":"springboot1",type:"工具书", user:{id:b+7, name:'张三'}},
		{"name":"springboot2",type:"语文书", user:{id:b+8, name:'李四'}},
		{"name":"springboot3",type:"数学书", user:{id:b+9, name:'王五'}},
		
		{"name":"springboot1",type:"工具书", user:{id:b+10, name:'张三'}},
		{"name":"springboot2",type:"语文书", user:{id:b+12, name:'李四'}},
		{"name":"springboot3",type:"数学书", user:{id:b+13, name:'王五'}},
		{"name":"springboot1",type:"工具书", user:{id:b+14, name:'张三'}},
		{"name":"springboot2",type:"语文书", user:{id:b+15, name:'李四'}},
		{"name":"springboot3",type:"数学书", user:{id:b+16, name:'王五'}},
		{"name":"springboot1",type:"工具书", user:{id:b+17, name:'张三'}},
		{"name":"springboot2",type:"语文书", user:{id:b+18, name:'李四'}},
		{"name":"springboot3",type:"数学书", user:{id:b+19, name:'王五'}},
		
		
		{"name":"springboot1",type:"工具书", user:{id:b+20, name:'张三'}},
		{"name":"springboot2",type:"语文书", user:{id:b+21, name:'李四'}},
		{"name":"springboot3",type:"数学书", user:{id:b+31, name:'王五'}},
		{"name":"springboot1",type:"工具书", user:{id:b+4, name:'张三'}},
		{"name":"springboot2",type:"语文书", user:{id:b+5, name:'李四'}},
		{"name":"springboot3",type:"数学书", user:{id:b+6, name:'王五'}},
		{"name":"springboot1",type:"工具书", user:{id:b+7, name:'张三'}},
		{"name":"springboot2",type:"语文书", user:{id:b+8, name:'李四'}},
		{"name":"springboot3",type:"数学书", user:{id:b+9, name:'王五'}},
		
		{"name":"springboot1",type:"工具书", user:{id:b+10, name:'张三'}},
		{"name":"springboot2",type:"语文书", user:{id:b+12, name:'李四'}},
		{"name":"springboot3",type:"数学书", user:{id:b+13, name:'王五'}},
		{"name":"springboot1",type:"工具书", user:{id:b+14, name:'张三'}},
		{"name":"springboot2",type:"语文书", user:{id:b+15, name:'李四'}},
		{"name":"springboot3",type:"数学书", user:{id:b+16, name:'王五'}},
		{"name":"springboot1",type:"工具书", user:{id:b+17, name:'张三'}},
		{"name":"springboot2",type:"语文书", user:{id:b+18, name:'李四'}},
		{"name":"springboot3",type:"数学书", user:{id:b+19, name:'王五'}},
		
	]
);


		b = b+30
}











//删除

//(1)删除
db.msg.remove({type:"工具书"})
//(2)删除所有
db.msg.remove({})

db.msg.remove({"json.id":1})




//修改
//(1)update首条
//update:遇到满足条件的第一条数据修改
db.msg.update({"name":"springboot"},{$set:{"name":"springboot2"}})

//(2)update多条
//updateMany :修改所有满足条件的
db.msg.updateMany({"name":"springboot"},{$set:{"name":"springboot2"}})

db.msg.updateMany({"json.id":1},{$set:{"name":"springboot2"}})


//(3)updateOne
//updateOne:修改满足条件的第一条数据
db.msg.updateOne({"name":"springboot2"},{$set:{"name":"springboot3"}})











//查询所有数据2种写法
db.msg.find()
db.getCollection("msg").find()


//查询数量
db.msg.find().itcount()


//条件查询
db.msg.find({type:"工具书"})

db.msg.find({"json.id":1})



//分页查询   
//第一页  每页3条
db.msg.find({"user.id":1}).skip(0).limit(3)

//第二页  每页3条
db.msg.find().skip(3).limit(3)


//第三页  每页3条
db.msg.find().skip(6).limit(3)



//排序
db.msg.find({"user.name":'张三'}).skip(200000).sort({ 'user.id':-1}).limit(30)

db.msg.find({"user.name":'张三'}).sort({ 'createDate':-1}).skip(0).limit(100)
db.msg.find({"user.id":99994}).sort({ 'createDate':-1}).skip(0).limit(100)
db.msg.find().sort({ 'createDate':1}).skip(0).limit(100)

db.msg.find({'createDate':{$gt:ISODate("2023-08-24T03:43:37Z")}}).sort({ 'createDate':1}).skip(0).limit(100)

db.msg.find({'createDate':{$gt:ISODate("2023-08-24T03:43:37Z")}}).count()


//条件
db.msg.find({"user.name":'张三',  'indexa': {$gt:3}}).skip(1000).sort({'indexa':-1}).limit(3)


//聚合查询
db.msg.aggregate({"$group":{"_id":"$type"
	,"total":{"$sum":"$user.id"}
	,"avg":{"$avg":"$user.id"}}})
相关推荐
d***9351 小时前
springboot3.X 无法解析parameter参数问题
android·前端·后端
q***71012 小时前
Spring Boot(快速上手)
java·spring boot·后端
n***84072 小时前
十七:Spring Boot依赖 (2)-- spring-boot-starter-web 依赖详解
前端·spring boot·后端
better_liang4 小时前
每日Java面试场景题知识点之-分布式事务处理
java·微服务·面试·springcloud·分布式事务
马克学长5 小时前
SSM青岛恒星科技学院机房管理系统0k0u9(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面
数据库·科技·ssm 框架·教育信息化·机房管理系统·青岛恒星科技学院
爱学习的小可爱卢5 小时前
JavaEE进阶——SpringMVC响应处理详解
spring boot·postman·javaee
q***96586 小时前
Spring Cloud Data Flow 简介
后端·spring·spring cloud
7***68436 小时前
Spring Boot 从 2.7.x 升级到 3.3注意事项
数据库·hive·spring boot
L***d6706 小时前
Spring Boot 各种事务操作实战(自动回滚、手动回滚、部分回滚)
java·数据库·spring boot
凌波粒6 小时前
Springboot基础教程(3)--自动装配原理/静态资源处理/欢迎页
java·spring boot·后端