【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"}}})
相关推荐
易云码5 分钟前
信息安全建设方案,网络安全等保测评方案,等保技术解决方案,等保总体实施方案(Word原件)
数据库·物联网·安全·web安全·低代码
向阳12188 分钟前
Dubbo负载均衡
java·运维·负载均衡·dubbo
newxtc10 分钟前
【客观理性深入讨论国产中间件及数据库-科创基础软件】
数据库·中间件·国产数据库·国产中间件·科创
水月梦镜花13 分钟前
redis:list列表命令和内部编码
数据库·redis·list
Gu Gu Study18 分钟前
【用Java学习数据结构系列】泛型上界与通配符上界
java·开发语言
WaaTong41 分钟前
《重学Java设计模式》之 原型模式
java·设计模式·原型模式
m0_7430484441 分钟前
初识Java EE和Spring Boot
java·java-ee
AskHarries43 分钟前
Java字节码增强库ByteBuddy
java·后端
佳佳_1 小时前
Spring Boot 应用启动时打印配置类信息
spring boot·后端
MonkeyKing_sunyuhua1 小时前
ubuntu22.04 docker-compose安装postgresql数据库
数据库·docker·postgresql