public bigdata

몽고 DB 기초 본문

컴퓨터, IT제품 Tip/DB

몽고 DB 기초

public bigdata 2019. 12. 11. 14:44

<기초 참고 자료>

https://uiandwe.tistory.com/969

 

8-3 mongodb 기초 쿼리

mongodb 는 기존 rdb와는 구조부터가 다릅니다. 데이블(mongodb 에서는 컬렉션이라 부릅니다만 테이블로 통용하겠습니다.) 의 foreign key 가 없기 때문에 테이블간의 join 이 어려우며 비정형테이블이기 때문에..

uiandwe.tistory.com

<DB 정렬 관련> - 인덱스 이외에는 32MB 이내로 제한되어 있다.

https://stackoverflow.com/questions/27023622/overflow-sort-stage-buffered-data-usage-exceeds-internal-limit

 

Overflow sort stage buffered data usage exceeds internal limit

Using the code: all_reviews = db_handle.find().sort('reviewDate', pymongo.ASCENDING) print all_reviews.count() print all_reviews[0] print all_reviews[2000000] The count prints 2043484, and it pr...

stackoverflow.com


<몽고 DB collection 리스트 확인>

show collections

<기본 Read query>

db.getCollection('BEST').findOne({"RNK":6}, {"NAME":1})
> RNK 값 6인 NAME 컬럼만 출력 / findOne은 하나만 매칭
# 일부만 출력
db.getCollection('BEST').find({"RNK":6}, {"NAME":1}).limit(5)
> RNK 값 6인 NAME 컬럼만 출력/ limit(5)로 

<특정한 변수만 선택>

db.getCollection('BEST').find({}, {RNK:1, NAME:1})

<정렬>

db.people.find( { status: "A" } ).sort( { user_id: 1 } )
> -1 : 내림차순

<결측이 아닌 값의 갯수>

db.people.find( { user_id: { $exists: true } } )

<중복을 제거한 값>

db.people.distinct( "status" )

<And 조건>

db.getCollection('BEST').find({"RNK":10, "TYPE" : "3"}).limit(5)

<OR 조건>

db.getCollection('BEST').find({$or : [{"RNK":10}, {"TYPE" : "3"}]}).limit(5)

<비교연산자>

db.getCollection('IMG_TAGS').find({"STATE":{$gt:3}})
> $gt, $gte, $lt, $lte 가능
db.people.find({ age: { $gt: 25, $lte: 50 } })
> 연산자 And 조건

<비교연산자 날짜 기준>

db.getCollection('PRODUCT').find({ 
    "REGIST_DT" :  
{ 
    "$gte" : ISODate("2019-12-01") 
    } 
})

<정규식 사용법>

db.people.find( { user_id: /bc/ } )
> “/”로 정규표현식 임을 알림