当前位置:网站首页>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}});
边栏推荐
- 随机数生成的四种方法|Random|Math|ThreadLocalRandom|SecurityRandom
- Android interview classic, 2022 Android interview written examination summary
- 实操演示:产研团队如何高效构建需求工作流?
- Leetcode brush questions: binary tree 11 (balanced binary tree)
- BZOJ 3747 POI2015 Kinoman 段树
- How to select the Block Editor? Impression notes verse, notation, flowus
- sun.misc.BASE64Encoder报错解决方法[通俗易懂]
- Debezium series: modify the source code to support UNIX_ timestamp() as DEFAULT value
- 【c语言】归并排序
- Analysis of openh264 decoded data flow
猜你喜欢
![[quick start to digital IC Verification] 8. Typical circuits in digital ICs and their corresponding Verilog description methods](/img/3a/7eaff0bf819c129b4f866388e57b87.png)
[quick start to digital IC Verification] 8. Typical circuits in digital ICs and their corresponding Verilog description methods

Convolution free backbone network: Pyramid transformer to improve the accuracy of target detection / segmentation and other tasks (with source code)

Autumn byte interviewer asked you any questions? In fact, you have stepped on thunder

.Net分布式事务及落地解决方案

港股将迎“最牛十元店“,名创优品能借IPO突围?

淺淺的談一下ThreadLocalInsecureRandom

Leetcode skimming: binary tree 12 (all paths of binary tree)

Let's talk about threadlocalinsecurerandom

无卷积骨干网络:金字塔Transformer,提升目标检测/分割等任务精度(附源代码)...

Debezium series: record the messages parsed by debezium and the solutions after the MariaDB database deletes multiple temporary tables
随机推荐
leetcode刷题:二叉树16(路径总和)
处理文件和目录名
【数字IC验证快速入门】9、Verilog RTL设计必会的有限状态机(FSM)
Debezium series: parsing the default value character set
Interviewer: what is the internal implementation of set data types in redis?
ACM getting started Day1
C - sequential structure
leetcode刷题:二叉树13(相同的树)
A way to calculate LNX
Scala basics [HelloWorld code parsing, variables and identifiers]
.Net分布式事務及落地解决方案
实操演示:产研团队如何高效构建需求工作流?
kubernetes资源对象介绍及常用命令(五)-(ConfigMap&Secret)
Summer Challenge harmonyos - realize message notification function
ffplay文档[通俗易懂]
Securerandom things | true and false random numbers
JS implementation prohibits web page zooming (ctrl+ mouse, +, - zooming effective pro test)
leetcode刷题:二叉树17(从中序与后序遍历序列构造二叉树)
[quick start of Digital IC Verification] 6. Quick start of questasim (taking the design and verification of full adder as an example)
-v parameter of GST launch