์์
db.employees.drop()
db.employees.insert({empno:7369 , ename : "SMITH", job : "CLERK", manager : "FORD", hiredate : "17-12-1980", sal : 800, deptno : 20 })
db.employees.insert({empno:7499 , ename : "ALLEN", job : "SALESMAN", manager : "BLAKE", hiredate : "20-02-1981", sal :1600, comm : 300, deptno : 30 })
db.employees.insert({empno:7521 , ename : "WARD", job : "SALESMAN", manager : "BLAKE", hiredate : "22-02-1981", sal : 1250, comm : 500, deptno : 30 })
db.employees.insert({empno:7566 , ename : "JONES", job : "MANAGER", manager : "KING", hiredate : "02-04-1981", sal : 2975, deptno : 20 })
db.employees.insert({empno:7654 , ename : "MARTIN", job : "SALESMAN", manager : "BLAKE", hiredate : "28-09-1981", sal : 1250, comm : 1400, deptno : 30 })
db.employees.insert({empno:7698 , ename : "BLAKE", job : "MANAGER", manager : "KING", hiredate : "01-05-1981", sal : 2850, deptno : 30 })
db.employees.insert({empno:7782 , ename : "CLARK", job : "MANAGER", manager : "KING", hiredate : "09-06-1981", sal : 2450, deptno : 10 })
db.employees.insert({empno:7788 , ename : "SCOTT", job : "ANALYST", manager : "JONES", hiredate : "13-06-1987", sal : 3000, deptno : 20 })
db.employees.insert({empno:7839 , ename : "KING", job : "CEO", manager : "", hiredate : "17-11-1981", sal : 5000, deptno : 10 })
db.employees.insert({empno:7844 , ename : "TURNER", job : "SALESMAN", manager : "BLAKE", hiredate : "08-09-1981", sal : 1500, deptno : 30 })
db.employees.insert({empno:7876 , ename : "ADAMS", job : "CLERK", manager : "SCOTT", hiredate : "13-06-1987", sal : 1100, deptno : 20 })
db.employees.insert({empno:7900 , ename : "JAMES", job : "CLERK", manager : "BLAKE", hiredate : "03-12-1981", sal : 950, deptno : 30 })
db.employees.insert({empno:7902 , ename : "FORD", job : "ANALYST", manager : "JONES", hiredate : "03-12-1981", sal : 3000, deptno : 20 })
db.employees.insert({empno:7934 , ename : "CLERK", job : "CLERK", manager : "KING", hiredate : "23-01-1982", sal : 1300, deptno : 10 })
db.employees.find()
๊ด๊ณํ ๋ฐ์ดํฐ ๋ฒ ์ด์ค : select* from empolyees
๋ชฝ๊ณ ๋๋น : db.employees.find({},{_id:0}).pretty()
-> ๋ชฝ๊ณ ๋๋น์์ ๋ถ์ฌํ _id๋ฅผ ์ ์ธํ์ฌ ์ค์ ๋ด๊ฐ ์ ๋ ฅํ ๊ฐ๋ง select
๊ด๊ณํ ๋ฐ์ดํฐ๋ฒ ์ด์ค : select ename, sal from employees
๋ชฝ๊ณ ๋๋น : db.employees.find({},{_id:0,ename:,sal:1})
db.ํ ์ด๋ธ๋ช .find({},{_id:0,์ถ๋ ฅํ๊ณ ์ถ์ key:1 ,์ถ๋ ฅํ๊ณ ์ถ์ key:1})
*_id๋ ๋ณ ๋ค๋ฅธ ๋ช ๋ น์ด ์์ผ๋ฉด ๊ธฐ๋ณธ์ ์ผ๋ก ์ถ๋ ฅ์ด ๋๋ค.
๊ด๊ณํ ๋ฐ์ดํฐ๋ฒ ์ด์ค : select ename, sal from employees where sal>=2000
๋ชฝ๊ณ ๋๋น : db.employees.find({sal:{$gte:2000}},{_id:0,ename:,sal:1})
* ํญ์ key, value ํํ๋ก ํํํ์ฌ์ผ ํจ.
๊ด๊ณํ ๋ฐ์ดํฐ๋ฒ ์ด์ค : select ename, sal from employees where sal>=1000 and sal<=3000
select ename, sal from employees where sal between 1000 and 3000
=> ๋์ผํ ๊ฒฐ๊ณผ
๋ชฝ๊ณ ๋๋น : db.employees.find({sal:{$gte:2000,$lte:3000}},{_id:0,ename:,sal:1})
๊ด๊ณํ ๋ฐ์ดํฐ ๋ฒ ์ด์ค :
create index ename_idx on employees(ename) -> ์ธ๋ฑ์ค ์์ฑ
select ename from employees where ename>='ALLEN' and ename < 'SCOTT'
๋ชฝ๊ณ ๋๋น :
db.employees.createIndex({ename:1}) -> ์ธ๋ฑ์ค ์์ฑ
* i ๋๋ฌธ์์ฌ์ผ ํจ!!!!!!!!!!!
db.employees.find({},{_id:0,ename:1,sal:1}).min({ename:'ALLEN'}).max({ename:'SCOTT'})
or
db.employees.find({ename:{$gte:'ALLEN',$lt:'SCOTT'}},{_id:0,ename:1,sal:1})
๊ด๊ณํ ๋๋น : select ename, sal from employees where sal <= 1000 or sal >=3000
๋ชฝ๊ณ ๋๋น : db.employees.find({$or:[{sal:{$lte:1000}},{sal:{$gte:3000}}]},{_id:0,ename:1,sal:1})
๊ด๊ณํ ๋ฐ์ดํฐ ๋ฒ ์ด์ค : select ename, sal,deptno from employees where deptno=10 and sal >=2000
๋ชฝ๊ณ ๋๋น :db.employees.find({$and:[{deptno:10},{sal:{$gte:2000}}]},{_id:0,ename:1,sal:1})
๊ด๊ณํ ๋ฐ์ดํฐ ๋ฒ ์ด์ค : select count(*) from employees
๋ชฝ๊ณ ๋๋น : db.employees.find().count()
๊ด๊ณํ : select deptno sum(sal) from employees grpup by deptno
๋ชฝ๊ณ : db.employees.group({key:{deptno:true},reduce:function(obj,prev){prev.csum+=obj.sal;},initial:{csum:0}})
: dept๊ฐ true์ธ ๊ฒ๋ง ๋ชจ์์(๊ฐ์ด ์๋๊ฒ, 10, 20, 30์ ๋ชจ์์ ) csum+=obj.sal ํด์ csum์ ์ถ๋ ฅํด์ค๋ค.
*reduce : ๊ฐ๊ฐ์ ๋ฐ์ดํฐ๋ฅผ ํตํฉ๋ ๋ฐ์ดํฐ๋ก ๋ง๋ค์ด ์ถ๋ ฅ ํด ์ค๋ค.
* ์ฐ์ฐ์ findํจ์๋ฅผ ํตํ์ฌ ์ถ๋ ฅํ๊ฒ๋๋ฉด ํธ์จ์ด ๋จ์ด์ ธ์ ๋งต ๋ฆฌ๋์ค๋ ์ด๋๋ฆฌ๊ฒ์ด์ ํ๋ ์์ํฌ๋ฅผ ํตํด ์ฐ์ฐํ๋ ๊ฒฝ์ฐ๊ฐ ๋ง๋ค.
๊ด๊ณํ : select ename, deptno,from employee where deptno in(10,30)
๋ชฝ๊ณ : db.employees.find({deptno:{$in:[10,30]}},{_id:0,ename:1,sal:1,deptno:1})
์๋ ์ฒ๋ผ comm์ด ์๋ ๋ฐ์ดํฐ์ ์๋ ๋ฐ์ดํฐ๊ฐ ์์ฌ ์์. ๋๊ฐ์ธ ๊ฒ๋ง ์ถ๋ ฅํ๊ฑฐ๋ ๋๊ฐ์ด ์๋ ๊ฒ๋ง ์ถ๋ ฅํ๊ฑฐ๋ ํ ์ ์๋ค.
๊ด๊ณํ : select ename, comm, sal from employees where comm is not null
๋ชฝ๊ณ : db.employees.find({comm:{$exists:true}},{_id:0,ename:1,sal:1,comm:1})
๊ด๊ณํ : select ename, sal from employees where comm is null
๋ชฝ๊ณ : db.employees.find({comm:{$exists:false}},{_id:0,ename:1,sal:1,comm:1})
db.createCollection("table_name") : ์ปฌ๋ ์ ์ ๋ง๋๋ ๋ช ๋ น์ด์ง๋ง ํฌ๊ฒ ์๋ฏธ์๋ค. ํ ์ด๋ธ์ ๊ฐ์ ๋ฃ์ผ๋ฉด ํ ์ด๋ธ์ด ์์ฑ ๋๋ค. ๊ทธ ์ธ ๊ด๊ณํ์์ ์ฐ๋ ALTER๋ ์๋ฏธ ์๋ค.
๋ชฝ๊ณ ๋๋น๋ ์ซ์์ ๊ธฐ๋ณธํ์ double ํ์ด๋ค.
-- comm ์ ํ์ ์ด double ์ธ ๊ฒ์ ๊ฒ์ db.employees.find({comm:{$type:1}},{empno:"", comm:""}) -- comm ์ ํ์ ์ด string ์ธ๊ฒ์ ๊ฒ์ db.employees.find({comm:{$type:2}},{empno:"", comm:""}) -- ename์ด S๋ก ์์ํด์ H๋ก ๋๋๋ ๊ฒ์ ๊ฒ์, 'i' : ๋์๋ฌธ์ ๊ตฌ๋ถ ์์ db.employees.find({ename:{$regex:'S.*H', $options: 'i'}},{empno:1, ename:1}) -- ename์ด S๋ก ์์ํด์ H๋ก ๋๋๋ ๊ฒ์ ๊ฒ์, 'i' : ๋์๋ฌธ์ ๊ตฌ๋ถ ์์ db.employees.find({ename:{$regex:'s.*h', $options: 'i'}},{empno:1, ename:1}) -- 'm' : ๋์๋ฌธ์ ๊ตฌ๋ถํจ b.employees.find({ename:{$regex:'s.*h', $options: 'm'}},{empno:1, ename:1}) db.employees.find({ename:{$regex:'S.*H', $options: 'm'}},{empno:1, ename:1})
๊ด๊ณํ : select ename from employee where ename like '%CL%'
๋ชฝ๊ณ : db.employees.find({ename:/CL/},{_id:0,ename:1})
๊ด๊ณํ : select ename from employees wgere ename like 'CL%'
๋ชฝ๊ณ : db.employees.find({ename:/^CL/},{_id:0,ename:1})
'db' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
mysql migration (0) | 2021.03.16 |
---|---|
mongodb mysql query๋ฌธ ๋น๊ต (0) | 2021.03.01 |
robo / mongodb ์ฐ๊ฒฐ (0) | 2021.03.01 |
mongoDB index์์ฑํ์ฌ ์ขํ์ฐ๊ธฐ (0) | 2021.02.27 |
MySQL ์ค๋ฅ๋ฉ์์ง (0) | 2020.11.09 |