当前位置:网站首页>Mongodb basic exercises
Mongodb basic exercises
2022-07-05 20:17:00 【Why not sell egg cakes well】
10 practice 1
// Access to database
user my_test
// Insert a document
db.user.insert({
username:"subway"
});
// Query the document
db.user.find();
// To the database user Insert a document into the collection
db.user.insert({
username:"zhengji"
});
// Inquire about user Document for collection
db.user.find();
// Count the number of collection documents
db.user.find().count();
// Add... To the document address attribute , The value is huaguoshan
db.users.insert({username:"sunwukong",{$set:{address:"huaguoshan"}}});
// Document replacement
db.users.resplaceOne({username:"zhubajie"},{username="tangsen"});
// Delete document properties
db.user.update({username:"sunwukong"},{$unset:{address:1}});
// Add a... To the document hobby by {} Properties of
// The attribute value of a document can also be a document , At this time, this document is ” Embedded documents “
db.users.update({username:"sunwukong",{$set:{...}}})
//mongodb Support query directly through embedded document properties
// If you want to query documents through embedded documents , Attribute names must be quoted
db.users.find({'hobby.movie':"hero"});
// towards tangsen Add a new movie to Intersteller
//$push Used to add new elements to the array
//$addToSet Add a new element to the array , But don't repeat
db.users.update({username:"tangsen"},{$push:{"hobby.movies":"Installer"}});
// Delete likes beijing Users of
db.users.remove({"hobby.cities":"beijing"});
// Delete user aggregate
db.users.remove({});
db.users.drop();
// towards numbers Insert 2000 Data
for(var i=1;i<=20000;i++){
db.numbers.insert({num:i});
}
// Performance optimization
var arr=[];
for(var i=1;i<=20000;i++){
arr.push({num:i});
}
db.numbers.insert(arr);
11 practice 2
Inquire about
// Inquire about numbers in num by 500 Documents
db.numbers.find({num:500})
// Inquire about numbers in num>500 Documents
db.numbers.find({num:{$gt:500}});
// Inquire about num Less than 30 Documents
db.numbers.find({num:{$lt:30}});
// The query is greater than 40 Less than 50
db.numbers.find({num:{$gt:40,$lt:50}});
// check numbers Before the assembly 10 Data
db.numbers.find({num:{$lte:10}});
//limit Set the upper limit of display data benefits : You will never execute queries without conditions during development
db.numbers.find().limit(10);
// Check before 11-20 strip
//skip Used to skip a certain amount of data before
//skip(( Page number -1)* Number of entries per page )
db.numbers.find().skip(10).limit(10);
//mongo Automatic adjustment skip and limit Location The front and rear positions have no effect
13 practice 3
// Import collection into database
// Select the... To import collection
// Query salary less than 2000 The employees'
db.emp.find({sal:{$lt:2000}});
// Check salary at 1000-2000 staff
db.emp.find({sal:{$gt:1000,$lt:2000}});
// Check that the salary is less than 1000 Or greater than 2500 staff
// Wrong way db.emp.find({sal:{$lt:1000,$gt:2500}});
db.emp.find({$or:[{sal:{$lt:10000},${gt:2500}}]});
// Check all employees in the finance department
var depno=db.dept.findOne({dname:" Finance Department "}).deptno;
db.emp.find({depno:depno});
// Query all mgr by 7698 All employees
db.emp.find({mgr:7698});
// Wages below 1000 Employee salary plus 400
db.emp.updateMany({sal:{$lte:1000}},{$inc:{sal:400}});
边栏推荐
- model方法
- After 95, Alibaba P7 published the payroll: it's really fragrant to make up this
- Oracle-表空间管理
- 银河证券在网上开户安全吗?
- ICTCLAS用的字Lucene4.9捆绑
- Leetcode brush questions: binary tree 18 (largest binary tree)
- 如何形成规范的接口文档
- Bzoj 3747 poi2015 kinoman segment tree
- Leetcode skimming: binary tree 10 (number of nodes of a complete binary tree)
- Leetcode brush questions: binary tree 11 (balanced binary tree)
猜你喜欢
随机推荐
Minimum commission for stock trading account opening, where to open an account with low commission? Is it safe to open an account on your mobile phone
Securerandom things | true and false random numbers
What is PyC file
股票开户哪里好?网上客户经理开户安全吗
如何形成规范的接口文档
ROS2专题【01】:win10上安装ROS2
leetcode刷题:二叉树10(完全二叉树的节点个数)
ICTCLAS word Lucene 4.9 binding
Debezium series: modify the source code to support drop foreign key if exists FK
Database logic processing function
[quick start of Digital IC Verification] 1. Talk about Digital IC Verification, understand the contents of the column, and clarify the learning objectives
C language OJ gets PE, OJ of ACM introduction~
Parler de threadlocal insecurerandom
Leetcode skimming: binary tree 16 (path sum)
Ffplay document [easy to understand]
Scala basics [HelloWorld code parsing, variables and identifiers]
When JS method passes long type ID value, precision loss will occur
无卷积骨干网络:金字塔Transformer,提升目标检测/分割等任务精度(附源代码)...
Oracle-表空间管理
死信队列入门(两个消费者,一个生产者)