728x90
반응형
더 많은 연습문제 보러가기 >>
Insert
db.collection.insertOne(
document
)
db.collection.insertMany([
document,
document,
!!...
])
* _id (primary key) : 명시하지 않으면 자동으로 ObjectId값 생성
# insertOne
db.qclass.insertOne({
name:'cho-gi',kor:100,eng:100,math:100}
)
# insertMany
db.qclass.insertMany([
{name:'jang-sy',kor:100,eng:100,math:100},
{name:'kim-',kor:90,eng:90,math:90},
{name:'choi-hs',kor:90,eng:90,math:90}
])
## “insert”를 javascript function으로 구현
function insertQclass(){
var studens = db.qclass.insertMany([{name:’bird’},{name:’pig’},{name:’dog’},{name:’cat’}])
}
Find
db.collection.find(query, projection)
- query : query selector
- projection : 출력할 field 결정. (1: true / 0: false)
var cursor = db.collection.find()
- find()를 통해 리턴되는 cursor를 var 변수(js)에 저장할 수 있다.
- hasNext(), forEach(), toArray() 등을 사용하여 cursor 내부의 document들을 사용할 수 있다.
# 전체출력
db.qclass.find({})
# 이름만 출력
db.qclass.find({},{name:1})<!--{조건,true/fail}--!>
_id가 보이지 않게 만드는거
db.qclass.find({},{_id:0,name:1})
# midterm이 있는 document 출력
db.qclass.find({},{midterm:1})
db.qclass.find({},{_id:0,midterm:1}) # id제외하고 출력
# 클래스가 qclass인 document 출력
db.qclass.find({class:'qclass'})
# midterm이 있는 document 출력
db.qclass.find({midterm:{$exists:true}})
# midterm의 국어점수가 50점보다 높은 document 출력
db.qclass.find({'midterm.kor':{$gt:50}})
# 국어점수가 >=50,영어<100인 document 출력
db.qclass.find({kor:{$gte:50},eng:{$lt:100}})
# 오름차순 정렬
db.qclass.find({},{_id:0}).sort({name:1})
# 내림차순 정렬
db.qclass.find({},{_id:0}).sort({name:-1})
#국어점수 내림차순
db.qclass.find().sort({kor:-1})
# 국어점수가 제일 높은 한명
db.qclass.find().sort({kor:-1}).limit(1)
# 1번 제외하고 그 다음부터 보자.
db.qclass.find().sort({kor:-1}).skip(1)
# 2등만 출력
db.qclass.find().sort({kor:-1}).skip(1).limit(1)
# qclass에서 영어점수가 가장 높은 사람 1명의 이름만 출력하자
db.qclass.find({},{_id:0, name:1}).sort({eng:-1}).limit(1)
Update
db.collection.updateOne(filter, update, options) - document의 field 수정
db.collection.updateMany(filter, update, options)
db.collection.replaceOne(filter, update, options) - document를 수정
- filter : 수정할 document를 find()
- update : update operator or aggregation pipeline
- options : 추가적인 기능 (upsert, writeConcern, !!...)
# updateOne
## qclass에서 name이 hong-gd을 찾아서 name을 홍길동 으로 바꾸자
db.qclass.update({name:/hong/},{$set:{name:”홍길동”}})
## NAME 이 lee-ss name을 ‘이순신’으로 바꾸자
db.qclass.updateOne({name:’lee-ss’},{$set:{name:’이순신’}})
# updateMany
## midterm이 존재하는 doc의 class를 aclass로 바꾸자
db.qclass.updateMany({midterm:{$exists:true}},{$set{class:'aclass'}})
## “update”를 javascript function으로 구현
function updateKor(){
var tmp = db.qclass.updateMany({kor:{$lte:60}},{$set:{kor:0}})
return tmp
}
# replaceOne
db.qclass.replaceOne({final:{$exists:true}},{name:’lee-ss’, kor:100,eng:0,math:50})
Delete
db.collection.deleteOne(filter, options)
db.collection.deleteMany(filter, options)
- filter : 삭제할 document를 find()
- options : 추가적인 기능 (writeConcern, collation)
# 홍길동만 삭제하자
db.qclass.deleteOne({name:'홍길동'})
# class가 존재하는 모든 doc을 삭제하자
db.qclass.deleteMany({class:{$exits:true}})
# 모든 doc을 삭제하자
db.qclass.deleteMany({},{_id:1})
db.qclass.deleteMany({})
▶︎ .drop(): collectio전체 삭제
▶︎ .sort(): 정렬
▶︎ .limit(): 특정 부분 제외/ 원하는 갯수만
▶︎ .distinct(): 중복없이 출력
728x90
반응형
'Web > MongoDB' 카테고리의 다른 글
[MongoDB]Map Reduce (0) | 2020.10.15 |
---|---|
[MongoDB]aggregation (0) | 2020.10.15 |
[MongoDB]CRUD_연습문제 (0) | 2020.10.15 |
[MongoDB]Data Structure (0) | 2020.10.15 |
[mongoDB]NoSQL (0) | 2020.10.15 |