【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"}}})
相关推荐
穿林鸟35 分钟前
Spring Boot项目信创国产化适配指南
java·spring boot·后端
褚翾澜36 分钟前
Haskell语言的NoSQL
开发语言·后端·golang
oh,huoyuyan40 分钟前
火语言RPA--Sqlite-导入数据表格
数据库·sqlite·rpa
伏游43 分钟前
【BUG】生产环境死锁问题定位排查解决全过程
服务器·数据库·spring boot·后端·postgresql·bug
此木|西贝1 小时前
【设计模式】模板方法模式
java·设计模式·模板方法模式
wapicn991 小时前
手机归属地查询Api接口,数据准确可靠
java·python·智能手机·php
hycccccch2 小时前
Springcache+xxljob实现定时刷新缓存
java·后端·spring·缓存
爱的叹息2 小时前
SpringBoot集成Redis 灵活使用 TypedTuple 和 DefaultTypedTuple 实现 Redis ZSet 的复杂操作
spring boot·redis·bootstrap
wisdom_zhe2 小时前
Spring Boot 日志 配置 SLF4J 和 Logback
java·spring boot·logback
揣晓丹2 小时前
JAVA实战开源项目:校园失物招领系统(Vue+SpringBoot) 附源码
java·开发语言·vue.js·spring boot·开源