mongoDB shell 접속
mongosh
Document
use 데이터베이스명
데이터베이스 사용
없으면 만들어준다.
db
현재 사용중인 데이터베이스 확인
show dbs
서버 내의 데이터베이스 리스트
최소 한 개의 Document를 가진 DB만 출력!
db.컬렉션명.insertOne({key:value, ...})
컬렉션 안에 Document 추가
db.데이터베이스이름.find()
DB내의 도큐먼트 조회
Collection
db.createCollection(컬렉션명[, options])
Collection 생성
options 참고하기👇
https://www.mongodb.com/docs/manual/reference/method/db.createCollection/
db.createCollection() — MongoDB Manual
Docs Home → MongoDB Manual db.createCollection(name, options)Creates a new collection or view. For views, see also db.createView().Because MongoDB creates a collection implicitly when the collection is first referenced in a command, this method is used p
www.mongodb.com
show collections
collection들을 조회
db.컬렉션명.drop()
컬렉션 제거
db.컬렉션명.find()
컬렉션 안의 모든 document 리스트 확인
삭제
db.dropDatabase()
use 데이터베이스명을 한 DB를 지운다.
더미 데이터 생성
for(var i = 1; i <= 1000; i++) {db.post.insertOne({"username" : "user"+i, "images" : [{"path" : "test" + i + "jpg"}], "content" : "hi", "hashTags" : ["#안녕", "#love"], "userId" : i})};
Date
Date()
현재 date를 문자열로 반환
new Date()
현재 date를 Data 객체로 반환
Date 객체를 ISODate helper로 감싼다.
function | return |
new Date("<YYYY-mm-dd>") | ISODate |
new Date("<YYYY-mm-ddTHH:MM:ss>") | UTC에 지정된 날짜 시간으로 ISODate 반환 |
new Date("<YYYY-mm-ddTHH:MM:ssZ>") | UTC에 지정된 날짜 시간으로 ISODate 반환 |
new Date(<integer>) | ISODate instance 결과를 반환 |
Aggregation Operations
다수의 documents를 처리하고 계산된 결과를 반환
- 다수의 documents를 값으로 그룹화
- 그룹화된 데이터에 대해 작업을 수행하여 단일 결과값을 반환
- 시간에 따라 변화하는 데이터를 분석
$match | 필터링 $match: { size: "medium" } |
$group | 그룹 $group: { _id: "$name", totalQuantity: { $sum: "$quantity" } } |
$sum | 합계 { $sum: "$quantity" } |
aggregation operations를 수행하기 위해
- Aggregation pipelines
- 선호되는 방법
- Single purpose aggregation methods
- 간단하지만, aggregation pipeline 기능 부족
// https://www.mongodb.com/docs/manual/core/aggregation-pipeline/#std-label-aggregation-pipeline-examples
db.orders.insertMany( [
{ _id: 0, name: "Pepperoni", size: "small", price: 19,
quantity: 10, date: ISODate( "2021-03-13T08:14:30Z" ) },
{ _id: 1, name: "Pepperoni", size: "medium", price: 20,
quantity: 20, date : ISODate( "2021-03-13T09:13:24Z" ) },
{ _id: 2, name: "Pepperoni", size: "large", price: 21,
quantity: 30, date : ISODate( "2021-03-17T09:22:12Z" ) },
{ _id: 3, name: "Cheese", size: "small", price: 12,
quantity: 15, date : ISODate( "2021-03-13T11:21:39.736Z" ) },
{ _id: 4, name: "Cheese", size: "medium", price: 13,
quantity:50, date : ISODate( "2022-01-12T21:23:13.331Z" ) },
{ _id: 5, name: "Cheese", size: "large", price: 14,
quantity: 10, date : ISODate( "2022-01-12T05:08:13Z" ) },
{ _id: 6, name: "Vegan", size: "small", price: 17,
quantity: 10, date : ISODate( "2021-01-13T05:08:13Z" ) },
{ _id: 7, name: "Vegan", size: "medium", price: 18,
quantity: 10, date : ISODate( "2021-01-13T05:10:13Z" ) }
] )
// https://www.mongodb.com/docs/manual/core/aggregation-pipeline/#std-label-aggregation-pipeline-examples
db.orders.aggregate( [
// Stage 1: Filter pizza order documents by pizza size
{
$match: { size: "medium" }
},
// Stage 2: Group remaining documents by pizza name and calculate total quantity
{
$group: { _id: "$name", totalQuantity: { $sum: "$quantity" } }
}
] )
위에 name, quantity를 변수로 설정해서 group으로 묶음
참고 👇
https://www.mongodb.com/docs/manual/reference/method/Date/
Date() — MongoDB Manual
Docs Home → MongoDB Manual Date()Returns a date either as a string or as a Date object.Date() returns the current date as a string in mongosh.new Date() returns the current date as a Date object. mongosh wraps the Date object with the ISODate helper. The
www.mongodb.com
https://pro-self-studier.tistory.com/58
1. MongoDB 데이터 모델링, DB/Collection/Document 생성 및 제거
안녕하세요, 프로독학러 입니다. 이번 포스팅에서는 MongoDB 의 데이터를 모델링하는 방법에 대해 간단히 살펴보고, MongoDB Shell 안에서 Database, Collection, Document 를 생성하고 제거하는 명령어에 대해
pro-self-studier.tistory.com
https://www.youtube.com/watch?v=xBbSR7xU2Yw&t=456s
Mongo Shell로 Dummy Data 생성하기
Mongo Shell로 더미 데이터 생성하기 서버-사이드 개발을 하면서 테스트를 위해 필요한 것은 더미 데이터이다. MongoDB에서는 Mongo Shell 을 이용하여 쉽게 생성할 수 있도록 지원해준다. Mongo Shell에서
dainelpark.tumblr.com
'DB > MongoDB' 카테고리의 다른 글
[Ubuntu] MongoDB 설치 (0) | 2024.04.22 |
---|---|
MongoDB repository (0) | 2023.02.23 |
MongoDB (0) | 2023.01.13 |
MongoDB 설치 (0) | 2023.01.13 |