【sql】MongoDB 增删改查 高级用法
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({"json.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({"json.name":'张三'}).skip(2000).sort({_id:-1, name:1}).limit(3)
//条件
db.msg.find({"json.name":'张三', 'indexa': {$gt:3}}).skip(1000).sort({'indexa':-1}).limit(3)