Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
Tags
- nosql
- django-debug-toolbar
- AWS
- Spark
- SCALA APP
- coding with ai
- Eclipse
- ubuntu
- git
- pyspark
- mcponaws
- Python
- AWSKRUG
- Transit Gateway
- list
- tgw
- MongoDB
- Amazon
- AI
- VPC
- amazon q
- debug_toolbar
- debug toolbar
- django
- ai assistant
- devops
- 툴바안뜸
- json
Archives
- Today
- Total
STACKBASE
mongodb 사용법 본문
반응형
1. 'root' 권한 유저 생성
# admin 데이터베이스 선택
use admin
db.createUser({user:'root',
pwd:'password',
roles:['root']
2. mongodb 로그인
mongo -u 'username' -p 'password123' --authenticationDatase 'admin'
3. DB/Collection 생성
use createdatabse
# 데이터베이스 확인
show dbs
# collection 생성
db.createCollection("collectionName")
# collection 확인
show collection
db.collection_name.stats()
# collection capped 설정하여 collection 생성
# capped 옵션을 True로 추가하고, 사이즈를 지정해주면, 지정한 사이즈 만큼
# 생성된 공간에서 데이터를 저장(사이즈 초과시 기존 공간에 Overwrite)
db.createCollection("collectionName", {capped:true, size:10000})
# collection 삭제
db.collection_name.drop()
# collection 이름 변경
db.collection.renameCollection("rename")
4. CRUD 실습
1) Create(Insert)
# Insert One(Document를 한개씩 입력)
db.colllection_name.insertOne(
{
val:"name",
val2: 100,
val3: "Status"
}
)
# InsertMany(Document를 여러개씩 한꺼번에 입력)
db.collection_name.insertMany(
[
{val:"String", val2:100, val3: "String"},
{val:"String", val2:100, val3: "String"},
{val:"String", val2:100, val3: "String"},
{val:"String", val2:100, val3: "String"}
]
)
2) Read
#find / findOne (매칭되는 Document들을 반환)
db.collection_name.find() // 전체 document 출력
db.collection_name.find({}, {val1:1, val2:0, _id:0})
// 전체 document를 출력하는데 val1만 출력
// _id는 기본적으로 출력되는 값으로 0으로 설정하면 출력되지 않는다.
db.collection_name.find({val:"A"})
// val=A인 document만을 출력한다.
db.collection_name.find({val1:"A", val2:10})
// val1 == A and val2 == 10 만을 출력한다.
db.collection_name.find({$or, [ {val1:"A"}, {val2:100} ]})
// val1 == A Or val == 100 을 출력한다.
db.collection_name.find(val:/bc/
// bc문자열을 포함하은 document 출력
db.collection_name.find({val : {$regex: /bc/} })
// bc문자열을 포함하은 document 출력(%bc%)
db.collection_name.find({val: {$regex: /^bc/} })
// bc%으로 bc문자열을 포함하는 document 출력
db.collection_name.find({age:{$lt:30}}).limit(3)
// 조건에 맞는 document 3줄 출력
db.collection_name.find({val:10}).sort({val2:1})
// val==10인 document를 val2값으로 ASC 정렬
db.collection_name.find({val:10}).sort({val2:-1})
// val==10인 document를 val2값으로 ASC 정렬
2-1) 비교 문법
$eq : ==
$gt : >
$gte : >=
$lt : <
$lte : <=
$ne : !=
$in : 배열에 값이 포함되어있는지 비교
$nin : 배열에 값이 포함되어있지 않은지 비교
반응형