-
MongoDB Time Series Collection - 1mongoDB 2022. 3. 25. 17:02
목표
기존에 mongoDB는 TimeSeriesCollection을 지원하지 않다가 5.0 version에서 처음으로 등장하게 되었다. 이에 관련해서 공부를 해보려고 한다. 그리고 최종적으로는 개발하고자 하는 앱의 사용자 검색 history Data를 Time Series Data로 관리할 수 있는지 Feasibility를 보고 적용해보려고 한다.
Time Series Data
시간 간격에따라 측정되는 Data이다. 그리고 시간 간격에 따라 지속적으로 Data가 들어오므로 빈도수가 높을 수 있다. 가장 친숙한 예를 들자면 평일에 자주 보는 주식 호가 data, 매수/매도 data가 있다.
mongoDB는 기본적으로 Timeseries Data를 저장 및 처리하는데 효율적이지만 고성능 솔루션을 위해서는 Modeling을 제대로 해야만 했지만 이는 쉽지 않았다. 이에 따라서 time-series Collection이 등장하게 되었다.
Time Series Collection이란?
표면적으로는 그냥 다른 mongoDB collection과 차이가 없다. 다른 collection처럼 읽고 쓸 수 있고 createIndex도 가능하다. 하지만 내부적으로는 time series 작업에 최적화 되어있다.
내부적으로는 time series collectino을 생성하면 컬렉션과 추상화 레이어 역할을 하는 writable한 가상의 view가 자동으로 생성된다. 이 추상 레이어를 사용함으로써 데이터를 disk에 쓸 때 버킷 패턴에 맞게 쓰기 때문에 성능에 걱정 없이 raw form 형식의 하나의 document로 사용할 수 있다. 따라서, 이제 schema를 디자인하거나 data를 read / write할 때 이런 세부사항을 신경쓰지 않아도 된다. 즉, 복잡한 버킷 document 작업이 아닌 추상화 레이어로 작업할 수 있다.
※버킷 패턴: 기존에 mongoDB에서 time-series를 관리하기 위해 사용했던 방식 Building with Patterns: The Bucket Pattern (mongodb.com)
Building with Patterns: The Bucket Pattern
Over the course of this blog post series, we'll take a look at twelve common Schema Design Patterns that work well in MongoDB.
www.mongodb.com
Why MongoDB Time-Series Collection?
- 기존 Collection 사용 방식과 다르지 않게 사용이 가능하다. 즉, time-series collection의 성능을 위해 무엇인가 하지 않아도 알아서 해준다.
- 데이터베이스는 수집, 검색, 저장을 위한 Storage Schema를 최적화하고 효율적으로 저장할 수 있도록 기본 압축을 제공한다. (duplicated 최소화)
- nesting data with document, secondary indexes, aggregation ($lookup, $merge...) 등 기존 형식의 document에서 사용하던 기능들을 사용할 수 있다.
- nesting data: document안에 object형식으로 계속 data를 넣는 것 ( field: {field1: value1, field2: value2} )
- secondary index (compound index): Indexes — MongoDB Manual
- aggregation: Aggregation Pipeline Stages — MongoDB Manual
Limitation
mongoDB는 이번에 5.0에서 처음으로 time series collection을 제공하는데 이에 몇가지 제한사항이 있다.
- Delete는 아래의 사항을 만족해야 한다.
- query는 metaField 의 값만을 사용할 수 있다.
- delete 커맨드가 지워지는 documents의 수를 제한하지 않아서 delete를 사용하려면 justOne: false option으로 사용하거나 deleteMany() method를 이용해야 한다.
- Update는 아래의 사항을 만족해야 한다.
- query는 metaField의 값만을 사용할 수 있다.
- metaField의 값만을 update할 수 있다.
- update는 update operator를 포함해야만 한다. (Update Operators — MongoDB Manual)
- update 커맨드가 update 수를 제한하지 않아서 update를 사용하려면 multi: true option으로 사용하거나 updateMany() method를 이용해야 한다.
- update command는 upsert: true로해서는 안된다.
- Secondary Index는 metaField와 timeField로만 가능하다.
- metaField는 2d, 2dsphere, text index type을 지원하지 않는다.
- secondary index는 TTL, Unique, Partial properties를 지원하지 않는다.
- 지원하지 않는 기능
- reIndex
- Capped Collections
- Modification of Collection Type (기존 <-> timeseries 둘 다 지원하지 않는다.)
- Modification of granularity는 커지는 값으로만 가능하다 seconds -> minutes -> hours(반대는 불가능)
- Schema Validation
- Client-Side Field Level Encryption
- Sharding은 5.0.6 부터 지원한다. 하지만 이 때 granularity를 변경할 수 없다.
- 5.0.6 부터 system.buckets (sharding administration commands)을 지원한다.
- Aggregation $out and $merge는 결과를 얻을 수 없다.
- Transactions(atomic 작업)을 write 시에 지원하지 않는다. (read는 가능)
- Change Stream
- Database Triggers
- GraphQL APi
- Atlas Search
- Realm Sync
이제 time-series Collection을 어떻게 생성하고 사용하는지 알아보자
'mongoDB' 카테고리의 다른 글
MongoDB Time Series Collection - 3 (0) 2022.03.25 MongoDB Time Series Collection - 2 (0) 2022.03.25 MongoDB란 (2) 2022.03.24 Nodejs에서 mongoDB, mongoose Version Upgrade (0) 2022.03.09 local 환경에서 Mongodb replicaset 설정하기 (0) 2021.04.11