-
기본 QueryDatabase/MongoDB 2023. 2. 1. 20:29
Adhoc
애드혹(라틴어: Ad hoc 아드 혹[*])은 "이것을 위해" 즉 "특별한 목적을 위해서"라는 뜻의 라틴어로, 일반적으로 다음을 나타냅니다.
- 특정한 문제나 일을 위해 만들어진 관습적인 해결책
- 일반화할 수 없는 해결책
- 어떤 다른 목적에 적응시킬 수 없는 해결책
Adhoc Query
좀 여유롭게,혹은 특정한 형식 없이 사용할 수 있는 쿼리문입니다.
Adhoc Query Example
$ mongo > use ipron // Database Create > db.createCollection("test") // Collection Create > db.createCollection("book",{capped:true, size:6142800, max:10000}) // Collection Max Size : 6142800 byte // Collection Max Document : 10000 // capped : true ( size 나 max 초과시 가장 오래된 data 부터 삭제 후 신규데이터를 추가 ) > show dbs // DB 목록보기 > show collections // Collection 목록보기 > db.test.insert({"name":"abc", "id":"abcd"}) // test Collection에 삽입 > db.test.insert({"name2":"abc2", "id2":"abcd2"}) // test Collection에 삽입 > db.test.find() // test Collection 데이터 확인 ( find 내에 검색조건을 넣을 수 있다. ) > db.test.remove({"name":abc}) // 같은값이면 전부 삭제 > db.test.find().pretty() // test Collection 데이터 확인 ( Json을 이쁘게 보여주는 기능 ) > db.test.drop() // test Collection 삭제 > db.dropDatabase() // DB 삭제
insert
db.test.insert({"name":"abc", "id":"abcd"})
- test collection 에 insert 내부의 Json Format 인 Document를 추가
delete
db.test.remove({"name":abc})
- name 이 abc 인 document 일괄삭제
db.test.deleteOne({"name":abc})
- name 이 abc 인 document 1개 삭제
select
db.book.count()
- document의 수량
db.book.find({"hits":{$gte:50}}).count()
- hits가 50 이상인 document의 수량
db.book.find()
- document 목록 조회
db.book.find({"hits":{$gte:50}}).pretty();
- hits가 50 이상인 document를 조회
- $gte는 greater than or equals(
보다 크거나 같거나), $lte는 less than or equals(보다 작거나 같거나)
db.book.find({"hits":{$gt:40, $lt:70}}).pretty();
- hits가 40 초과 70 미만
- $gt(
보다 큰), $lt(보다 작은)
db.book.find({"name":{$regex:/a|b/,$options:"i"}}).pretty()
db.book.find({"name":/a|b/i}).pretty()- a또는 b 를 정규식으로 검색 option: i는 대소문자 무시
db.book.find({$where: "this.name == ‘A’"}).pretty()
- $where을 이용하면 자바스크립트 표현식(expression)을 사용
db.book.find({$and:[{$where:"this.auther.length == 2"}, {"name":"c"}]}).pretty()
- auther의 Field가 2개 이며 name이 c인 Document를 검색
db.book.find({"auther":{$elemMatch:{"name":"park"}}}).pretty();
- embedded Document란 auther Field처림 Document 안 배열 형태로 있는 Document 를 말합니다. $elemMatch는 이러한 것들을 검색 할 때 이용합니다.
db.book.insert({"name":"D","hits":200,"auther":[], "language": [ "eng","kor","jpn" ],"seller":{"name":"saramin"}})
- Embedded Document배열이 아닌경우 접근을 해보기 위해 mock 데이터 추가
db.book.find({"language":"eng"}).pretty()
- Embedded Document가 아니고 배열일 경우 바로 접근하면 된다.
db.book.find("seller.name":"saramin").pretty()
- Embedded Document가 아니고 Key/Field일 경우도 .형태로 접근 가능
db.book.find({},{"_id":false,"name":true,"hits":true});
- 2번째 인자값은 option으로 보여질 Field에 대한 결정을 함
db.book.find({$and:[{"name":"A"}]},{"auther":{$slice:1}}).pretty()
-$slice를 하면 해당 갯수만큼의 Document만큼만 갖고온다.db.book.find().sort({"hits":1}).pretty()
- 1은 오름차순, -1은 내림차순
db.book.find().limit(2).pretty()
- 2개만 출력
db.book.find().skip(2)
- 리스트에서 2개를 skip후 Document를 출력
update
db.book.update({"hits":110},{$set:{"hits":120}})
- update 첫번째인자는 검색 key/field,
- update 두번째인자는 업데이트값 {$set: {key:field}}
- update 세번째인자는 조건에 해당하는 값이 없을때 동작방식 ( true : Insert, false : ignore )
- 해당 테이블 전체에 적용여부 ( true/false , default : false )
db.book.update({"hits":120},{hits:125,name:"AA"})
- hits를 120에서 125로 업데이트를 하며 기존에 name이 없었는데 AA를 추가
db.book.update({name:"F"},{name:"F",hits:20}),{upsert:true})
- upsert : 값이 없을 경우 insert 아니면 update
db.book.update({hits:{$lte:30}},{$set:{bestseller:"N"}},{multi:true})
- 여러 행을 업데이트 할 경우 {multi:true}로 설정
db.book.update({name:"F"},{$push:{category:"science"}})
- $push를 이용하여 category라는 field에 science라는 배열 추가
db.book.update({name:"F"},{$pull:{category:"science"}})
- $pull을 이용하여 science 배열 값 제거
'Database > MongoDB' 카테고리의 다른 글
Index (1) 2023.02.02 MapReduce (0) 2023.02.02 Operator & Function (0) 2023.02.01 Aggregate (0) 2023.02.01 What is MongoDB? (0) 2023.02.01