db

mongoDB ํ™œ์šฉํ•˜๊ธฐ(CRUD)

dev_summer 2021. 2. 27. 15:08

 

์˜ˆ์ œ

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