MongoDB command combat

// 1. Dept and emp set into the database
(data import process is omitted here)

db.dept.find()
db.emp.find()

// 2. Queries wages of less than 2000 employees

db.emp.find({sal:{$lt:2000}});

// 3. Queries wage employees between 1000-2000

db.emp.find({sal:{$lt:2000 , $gt:1000}});

// 4. Query wages of less than 1000 or greater than 2500 employees

db.emp.find({$or:[{sal:{$lt:1000}} , {sal:{$gt:2500}}]});

5 // Query the Ministry of Finance of all employees
// (depno)

var depno = db.dept.findOne({dname:"财务部"}).deptno;
db.emp.find({depno:depno});

6 // query for all employees in the sales department

var depno = db.dept.findOne({dname:"销售部"}).deptno;
db.emp.find({depno:depno});

// 7. Mgr for all employees to query all of 7698

db.emp.find({mgr:7698})

// 8. Below the 1000 increase of wages for all employees pay 400 yuan

db.emp.updateMany({sal:{$lte:1000}} , {$inc:{sal:400}});
db.emp.find()

// 9. Wages by ascending or descending order
/ query document, the default is sorted (ascending order) according to the value of _id
sort () can be used to specify a document collation, sort () need to pass an object to specify the sort [rule 1: indicates ascending -1: indicates descending]
/

db.emp.find({}).sort({sal:1})      //升序
db.emp.find().sort({sal:-1})     //降序

// 10. Wage press ascending order, if the same salary, department number descending press arrangement
(can transmit a plurality of collation)

db.emp.find().sort({sal:1,empno:-1})

// 11 Show only the employee name and salary, do not show _id (_id default will be displayed)
* in the query, you can set the projector position results in the second parameter *

db.emp.find({},{ename:1,_id:0,sal:1})

PS: limit Skip the Sort can be called in any order, generally always to emphasize the Sort .

Published 12 original articles · won praise 0 · Views 166

Guess you like

Origin blog.csdn.net/qq_44571236/article/details/103968165