Dev_from the Bottom

#27. MongoDB_03 : DB, Collection 수정 삭제/ 상태 확인 / 반복문 활용 데이터 입력 본문

Database

#27. MongoDB_03 : DB, Collection 수정 삭제/ 상태 확인 / 반복문 활용 데이터 입력

고무라면 2022. 5. 31. 05:02

1. DB 상태 확인

db.enableFreeMonitoring()

>>>
{
  state: 'enabled',
  message: 'To see your monitoring data, navigate to the unique URL below. Anyone you share the URL with will also be able to view this page. You can disable monitoring at any time by running db.disableFreeMonitoring().',
  url: 'https://cloud.mongodb.com/freemonitoring/cluster/MPG7YVNN5QYDFSL2IDTPDOF3TPNBUVEV',
  userReminder: '',
  ok: 1
}

 

 

 

2. capped 설정하여, 컬랙션 생성

  • capped collection : 제한된 크기로 생성된 공간 안에서만 데이터를 저장할 수 있고, 최초 공간이 모두 할당되면, 다시 처음으로 돌아가서 기존 공간을 재사용하는 타입의 Collection
  • 로그 데이터처럼 일정한 기간 내에서만 저장, 관리할 필요가 있는 데이터에 효과적
  • capped: true로 지정할 시, 반드시 'size' 옵션으로 최대값을 설정해야 함
# capped 설정해서 Collection 명시적 생성

db.createCollection("col_test01", {capped:true, size:500})

>>>
{ ok: 1 }

 

3. Collection 상태 및 토탈 사이즈 확인

# 컬랙션 상태 확인 : 결과값이 많음
db.col2.stats()

# total size 확인
db.col2.totalSize

>>>
[Function: totalSize] AsyncFunction {
  apiVersions: [ 0, 0 ],
  returnsPromise: true,
  serverVersions: [ '0.0.0', '999.999.999' ],
  topologies: [ 'ReplSet', 'Sharded', 'LoadBalanced', 'Standalone' ],
  returnType: { type: 'unknown', attributes: {} },
  deprecated: false,
  platforms: [ 0, 1, 2 ],
  isDirectShellCommand: false,
  acceptsRawInput: false,
  shellCommandCompleter: undefined,
  help: [Function (anonymous)] Help
}

 

4. 반복문 활용하여 데이터 삽입

  • 반복문 문법은 JAVA와 같은 듯
# 반복문 활용하여 데이터 입력
for(i = 0; i < 20; i++){
  db.col1.insertOne({a:i})
}
>>>
{ acknowledged: true,    # 성공
  insertedId: ObjectId("629546ef44f721d802a68c88") }
  
# 결과 확인
db.col1.find()

>>>

{ _id: ObjectId("629546ed44f721d802a68a95"), a: 0 }
{ _id: ObjectId("629546ed44f721d802a68a96"), a: 1 }
{ _id: ObjectId("629546ed44f721d802a68a97"), a: 2 }
{ _id: ObjectId("629546ed44f721d802a68a98"), a: 3 }
{ _id: ObjectId("629546ed44f721d802a68a99"), a: 4 }
{ _id: ObjectId("629546ed44f721d802a68a9a"), a: 5 }
{ _id: ObjectId("629546ed44f721d802a68a9b"), a: 6 }
{ _id: ObjectId("629546ed44f721d802a68a9c"), a: 7 }
{ _id: ObjectId("629546ed44f721d802a68a9d"), a: 8 }
{ _id: ObjectId("629546ed44f721d802a68a9e"), a: 9 }
{ _id: ObjectId("629546ed44f721d802a68a9f"), a: 10 }
{ _id: ObjectId("629546ed44f721d802a68aa0"), a: 11 }
{ _id: ObjectId("629546ed44f721d802a68aa1"), a: 12 }
{ _id: ObjectId("629546ed44f721d802a68aa2"), a: 13 }
{ _id: ObjectId("629546ed44f721d802a68aa3"), a: 14 }
{ _id: ObjectId("629546ed44f721d802a68aa4"), a: 15 }
{ _id: ObjectId("629546ed44f721d802a68aa5"), a: 16 }
{ _id: ObjectId("629546ed44f721d802a68aa6"), a: 17 }
{ _id: ObjectId("629546ed44f721d802a68aa7"), a: 18 }
{ _id: ObjectId("629546ed44f721d802a68aa8"), a: 19 }

5. DB 정보, DB 내 Collection 정보, 서버 정보 확인 명령어

# DB 정보 확인
db.stats()

# DB 내 컬랙션 정보 확인
db.getCollectionInfos()

# 서버 정보 확인
db.serverStatus()

# 참고

Comments