当前位置:网站首页>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}});
边栏推荐
- Wechat applet regular expression extraction link
- 2022年7月4日-2022年7月10日(ue4视频教程mysql)
- leetcode刷题:二叉树18(最大二叉树)
- c語言oj得pe,ACM入門之OJ~
- 1: Citation;
- Oracle tablespace management
- 信息学奥赛一本通 1337:【例3-2】单词查找树 | 洛谷 P5755 [NOI2000] 单词查找树
- 95后阿里P7晒出工资单:狠补了这个,真香...
- Debezium series: idea integrates lexical and grammatical analysis ANTLR, and check the DDL, DML and other statements supported by debezium
- 炒股开户最低佣金,低佣金开户去哪里手机上开户安全吗
猜你喜欢

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

Elk distributed log analysis system deployment (Huawei cloud)

Let's talk about threadlocalinsecurerandom

Debezium series: record the messages parsed by debezium and the solutions after the MariaDB database deletes multiple temporary tables

零道云新UI设计中

Oracle-表空间管理
![[quick start of Digital IC Verification] 6. Quick start of questasim (taking the design and verification of full adder as an example)](/img/6d/110b87747f0a4be52da9fd49a05b82.png)
[quick start of Digital IC Verification] 6. Quick start of questasim (taking the design and verification of full adder as an example)

【数字IC验证快速入门】8、数字IC中的典型电路及其对应的Verilog描述方法

leetcode刷题:二叉树16(路径总和)

Leetcode skimming: binary tree 17 (construct binary tree from middle order and post order traversal sequence)
随机推荐
[quick start of Digital IC Verification] 1. Talk about Digital IC Verification, understand the contents of the column, and clarify the learning objectives
Base du réseau neuronal de convolution d'apprentissage profond (CNN)
Leetcode brush question: binary tree 13 (the same tree)
Debezium series: record the messages parsed by debezium and the solutions after the MariaDB database deletes multiple temporary tables
Leetcode skimming: binary tree 16 (path sum)
2023年深圳市绿色低碳产业扶持计划申报指南
解决Thinkphp框架应用目录下数据库配置信息修改后依然按默认方式连接
深度學習 卷積神經網絡(CNN)基礎
DP: tree DP
【c语言】归并排序
Leetcode brush questions: binary tree 18 (largest binary tree)
无卷积骨干网络:金字塔Transformer,提升目标检测/分割等任务精度(附源代码)...
Solve the problem that the database configuration information under the ThinkPHP framework application directory is still connected by default after modification
CADD课程学习(7)-- 模拟靶点和小分子相互作用 (半柔性对接 AutoDock)
【数字IC验证快速入门】3、数字IC设计全流程介绍
What is PyC file
S7-200smart uses V90 Modbus communication control library to control the specific methods and steps of V90 servo
95后阿里P7晒出工资单:狠补了这个,真香...
C langue OJ obtenir PE, ACM démarrer OJ
[C language] merge sort