当前位置:网站首页>mongodb基操的练习
mongodb基操的练习
2022-07-05 20:11:00 【为什么不好好卖蛋饼】
10 练习1
//进入数据库
user my_test
//插入一个文档
db.user.insert({
username:"subway"
});
//查询文档
db.user.find();
//向数据库user集合插入一个文档
db.user.insert({
username:"zhengji"
});
//查询user集合的文档
db.user.find();
//统计集合文档数量
db.user.find().count();
//文档中添加address属性,值为huaguoshan
db.users.insert({username:"sunwukong",{$set:{address:"huaguoshan"}}});
//文档替换
db.users.resplaceOne({username:"zhubajie"},{username="tangsen"});
//删除文档属性
db.user.update({username:"sunwukong"},{$unset:{address:1}});
//向文档中添加一个hobby为{}的属性
//文档的属性值也可以是一个文档,此时成这个文档为”内嵌文档“
db.users.update({username:"sunwukong",{$set:{...}}})
//mongodb支持直接通过内嵌文档属性进行查询
//如果要通过内嵌文档对文档进行查询,属性名必须加引号
db.users.find({'hobby.movie':"hero"});
//向tangsen中添加一个新的电影Intersteller
//$push用于向数组中添加新的元素
//$addToSet 向数组中添加一个新元素,但是不重复
db.users.update({username:"tangsen"},{$push:{"hobby.movies":"Installer"}});
//删除喜欢beijing 的用户
db.users.remove({"hobby.cities":"beijing"});
//删除user集合
db.users.remove({});
db.users.drop();
//向numbers插入2000条数据
for(var i=1;i<=20000;i++){
db.numbers.insert({num:i});
}
//性能优化的做法
var arr=[];
for(var i=1;i<=20000;i++){
arr.push({num:i});
}
db.numbers.insert(arr);
11 练习2
查询
//查询numbers中num为500的文档
db.numbers.find({num:500})
//查询numbers中num>500的文档
db.numbers.find({num:{$gt:500}});
//查询num小于30的文档
db.numbers.find({num:{$lt:30}});
//查询大于40小于50
db.numbers.find({num:{$gt:40,$lt:50}});
//查numbers集合的前10条数据
db.numbers.find({num:{$lte:10}});
//limit设置显示数据的上限 好处:开发时候绝对不会执行不带条件的查询
db.numbers.find().limit(10);
//查前11-20条
//skip用于跳过前一定数量的数据
//skip((页码-1)*每页条数)
db.numbers.find().skip(10).limit(10);
//mongo自动调整 skip和limit位置 前后位置没有影响
13 练习3
//导入集合到数据库
//选择要导入的collection
//查询工资小于2000的员工
db.emp.find({sal:{$lt:2000}});
//查询工资在1000-2000员工
db.emp.find({sal:{$gt:1000,$lt:2000}});
//查工资小于1000或大于2500员工
// 错误做法 db.emp.find({sal:{$lt:1000,$gt:2500}});
db.emp.find({$or:[{sal:{$lt:10000},${gt:2500}}]});
//查询财务部所有员工
var depno=db.dept.findOne({dname:"财务部"}).deptno;
db.emp.find({depno:depno});
//查询所有mgr为7698的所有员工
db.emp.find({mgr:7698});
//工资低于1000员工工资加上400
db.emp.updateMany({sal:{$lte:1000}},{$inc:{sal:400}});
边栏推荐
猜你喜欢
【数字IC验证快速入门】2、通过一个SoC项目实例,了解SoC的架构,初探数字系统设计流程
Leetcode skimming: binary tree 17 (construct binary tree from middle order and post order traversal sequence)
零道云新UI设计中
[C language] string function and Simulation Implementation strlen & strcpy & strcat & StrCmp
Base du réseau neuronal de convolution d'apprentissage profond (CNN)
Elk distributed log analysis system deployment (Huawei cloud)
14. Users, groups, and permissions (14)
淺淺的談一下ThreadLocalInsecureRandom
【数字IC验证快速入门】8、数字IC中的典型电路及其对应的Verilog描述方法
Leetcode brush question: binary tree 13 (the same tree)
随机推荐
How to select the Block Editor? Impression notes verse, notation, flowus
零道云新UI设计中
关于BRAM IP复位的优先级
Securerandom things | true and false random numbers
【c语言】快速排序的三种实现以及优化细节
After 95, Alibaba P7 published the payroll: it's really fragrant to make up this
深度学习 卷积神经网络(CNN)基础
Solve the problem that the database configuration information under the ThinkPHP framework application directory is still connected by default after modification
Zero cloud new UI design
leetcode刷题:二叉树14(左叶子之和)
解决php无法将string转换为json的办法
怎么挑选好的外盘平台,安全正规的?
Win10 x64环境下基于VS2017和cmake-gui配置使用zxing以及opencv,并实现data metrix码的简单检测
无卷积骨干网络:金字塔Transformer,提升目标检测/分割等任务精度(附源代码)...
sun. misc. Base64encoder error reporting solution [easy to understand]
CADD课程学习(7)-- 模拟靶点和小分子相互作用 (半柔性对接 AutoDock)
Analysis of openh264 decoded data flow
Guidelines for application of Shenzhen green and low carbon industry support plan in 2023
Is it safe for Guosen Securities to open an account online?
Notes on key vocabulary in the English original of the biography of jobs (12) [chapter ten & eleven]