0Pricing
MongoDB Academy · 강의

시계열 데이터 삽입 및 쿼리하기

학습자는 측정값을 일괄 삽입하고 시간 범위 및 메타데이터 필드에 필터를 적용해 쿼리합니다.

시계열 데이터 삽입 및 쿼리하기은(는) CoddyKit의 무료 MongoDB Academy 강의입니다. 이것은 4개 중 2번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 MongoDB Academy 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. MongoDB Academy 강의에는 총 4개의 강의가 포함되어 있습니다.

이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.

Inserting Measurements Into Time Series

Inserting into a time series collection uses the exact same insertOne() and insertMany() methods as regular collections. MongoDB inspects the timeField (which must be a BSON Date) and the metaField to place the measurement into the correct internal bucket. The insert API is intentionally identical so existing application code requires minimal changes when adopting time series collections.

const now = new Date()

db.sensorReadings.insertOne({
  timestamp: now,
  sensorId: 'sensor-42',
  temperature: 23.1,
  humidity: 58.4,
  batteryLevel: 87
})

Bulk Inserting Historical Data

When loading historical measurements, always prefer insertMany() over repeated insertOne() calls. MongoDB can batch the data into buckets far more efficiently with bulk operations. For very large datasets (millions of rows), consider using mongoimport or the Node.js driver's bulkWrite() with insertOne operations grouped in batches of 1,000–5,000 documents.

const readings = []
const base = new Date('2024-06-01T00:00:00Z')

for (let i = 0; i < 1440; i++) {
  readings.push({
    timestamp: new Date(base.getTime() + i * 60000),
    sensorId: 'sensor-42',
    temperature: 20 + Math.random() * 5,
    humidity: 50 + Math.random() * 20
  })
}

db.sensorReadings.insertMany(readings)

Basic Time-Range Queries

The most common query pattern for time series data is a time-range filter on the timeField using $gte and $lte. MongoDB uses the internal bucket boundaries to skip entire buckets that fall outside the requested range, achieving much better performance than scanning every document. Always include a time filter when querying large time series collections.

// Last 1 hour of readings from one sensor
const oneHourAgo = new Date(Date.now() - 60 * 60 * 1000)

db.sensorReadings.find({
  sensorId: 'sensor-42',
  timestamp: { $gte: oneHourAgo }
}).sort({ timestamp: 1 })

// Specific day range
db.sensorReadings.find({
  timestamp: {
    $gte: new Date('2024-06-01T00:00:00Z'),
    $lt:  new Date('2024-06-02T00:00:00Z')
  }
})

Filtering on Metadata Fields

Filtering on the metaField is highly optimised — MongoDB stores the meta value at the bucket level and can skip entire buckets belonging to other series without inspecting individual measurements. This makes queries like 'all readings from sensor-42 in the last 6 hours' extremely fast even on collections holding billions of measurements from thousands of sensors.

// Query a specific device
db.sensorReadings.find({
  sensorId: 'sensor-42',
  timestamp: { $gte: new Date('2024-06-01T00:00:00Z') }
})

// Query multiple devices using $in on the metaField
db.sensorReadings.find({
  sensorId: { $in: ['sensor-42', 'sensor-43', 'sensor-44'] },
  timestamp: { $gte: new Date('2024-06-01T00:00:00Z') }
})

Aggregating Time Series With $match and $group

The aggregation pipeline is the primary tool for computing statistics over time series data. A typical pattern is to $match on the time range and metaField first (so MongoDB can skip irrelevant buckets), then $group to compute averages, minimums, and maximums. Always place $match as the very first stage to enable bucket pruning.

// Average temperature per hour for one sensor
db.sensorReadings.aggregate([
  {
    $match: {
      sensorId: 'sensor-42',
      timestamp: { $gte: new Date('2024-06-01T00:00:00Z') }
    }
  },
  {
    $group: {
      _id: {
        year:  { $year: '$timestamp' },
        month: { $month: '$timestamp' },
        day:   { $dayOfMonth: '$timestamp' },
        hour:  { $hour: '$timestamp' }
      },
      avgTemp: { $avg: '$temperature' },
      maxTemp: { $max: '$temperature' },
      minTemp: { $min: '$temperature' }
    }
  },
  { $sort: { '_id.hour': 1 } }
])

Using $dateTrunc for Time Bucketing

The $dateTrunc aggregation expression (added in MongoDB 5.0) simplifies grouping measurements into fixed-width time windows. It truncates a date to the nearest unit boundary — for example, truncating to 'hour' groups all measurements within the same hour under the same key. This replaces the verbose multi-field date extraction approach.

// Group readings into 15-minute windows
db.sensorReadings.aggregate([
  {
    $match: {
      sensorId: 'sensor-42',
      timestamp: { $gte: new Date('2024-06-01T00:00:00Z') }
    }
  },
  {
    $group: {
      _id: {
        $dateTrunc: {
          date: '$timestamp',
          unit: 'minute',
          binSize: 15
        }
      },
      avgTemp: { $avg: '$temperature' },
      count:   { $sum: 1 }
    }
  },
  { $sort: { _id: 1 } }
])

Projecting Time Series Results

Use projection to limit the fields returned from time series queries, just as with regular collections. Projecting only the fields you need reduces network transfer and client memory usage. Note that the timeField and metaField are always available for projection, and the _id field can be suppressed with _id: 0.

// Return only timestamp and temperature
db.sensorReadings.find(
  {
    sensorId: 'sensor-42',
    timestamp: { $gte: new Date('2024-06-01T08:00:00Z') }
  },
  {
    _id: 0,
    timestamp: 1,
    temperature: 1
  }
).sort({ timestamp: 1 })

Counting and Sampling Measurements

Use countDocuments() with a filter to count measurements in a time range. For large collections, estimatedDocumentCount() provides a fast approximate total using collection metadata. When debugging or building dashboards, $sample in an aggregation pipeline lets you retrieve a random subset of measurements without scanning the full result set.

// Count readings in last 24 hours
const since = new Date(Date.now() - 86400000)
db.sensorReadings.countDocuments({
  sensorId: 'sensor-42',
  timestamp: { $gte: since }
})

// Random sample of 10 recent measurements
db.sensorReadings.aggregate([
  { $match: { timestamp: { $gte: since } } },
  { $sample: { size: 10 } }
])

Node.js Driver: Reading Time Series Data

In a Node.js application, querying time series collections is identical to querying regular collections. Use the standard find() or aggregate() methods on the collection object. Since time series queries often return large result sets, use async iteration over the cursor rather than loading everything into memory with toArray().

const { MongoClient } = require('mongodb')

async function getRecentReadings(client) {
  const db = client.db('iot')
  const col = db.collection('sensorReadings')
  const since = new Date(Date.now() - 3600000)  // last hour

  const cursor = col.find(
    { sensorId: 'sensor-42', timestamp: { $gte: since } },
    { projection: { _id: 0, timestamp: 1, temperature: 1 } }
  ).sort({ timestamp: 1 })

  for await (const doc of cursor) {
    console.log(doc.timestamp, doc.temperature)
  }
}

Performance: Why Time Range First

The golden rule of querying time series data is: always filter by time range before anything else. MongoDB's bucket pruning only activates when the timeField filter appears in the query. Without it, MongoDB must scan every bucket. Additionally, combine the time filter with the metaField filter to leverage both forms of bucket skipping — time boundaries and series identity.

// GOOD: time + meta filter — fast bucket pruning
db.sensorReadings.find({
  sensorId: 'sensor-42',       // prune by series
  timestamp: { $gte: since },  // prune by time
  temperature: { $gt: 30 }     // measurement filter applied after pruning
})

// BAD: no time filter — scans all buckets
db.sensorReadings.find({
  temperature: { $gt: 30 }     // forces full scan
})

Monitoring Bucket Utilisation

You can inspect the internal bucket documents using a system namespace: system.buckets.<collectionName>. While you cannot write to this namespace directly, reading it reveals how many buckets exist and how many measurements each bucket holds. This is useful when tuning granularity — ideally each bucket should be close to its maximum fill level (3,600 for 'seconds' granularity).

// Count internal bucket documents
db['system.buckets.sensorReadings'].countDocuments()

// Inspect a sample bucket (internal format)
db['system.buckets.sensorReadings'].findOne()

Quick Check

Test your understanding of MongoDB & NoSQL Databases concepts from this lesson.

Lesson Recap

In this lesson you learned: insertMany() with batches is the efficient way to load time series data, filtering by timeField and metaField together enables bucket pruning for fast range queries, and $dateTrunc in aggregation pipelines simplifies grouping measurements into fixed-width time windows. Next up we explore windowed aggregations over time series using $setWindowFields.

자주 묻는 질문

“시계열 데이터 삽입 및 쿼리하기” 강의는 무료인가요?

네 — “시계열 데이터 삽입 및 쿼리하기” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 MongoDB Academy 강의 전체를 잠금 해제할 수 있습니다. MongoDB Academy 강의에는 총 4개의 강의가 포함되어 있습니다.

“시계열 데이터 삽입 및 쿼리하기”에서 뭘 배우나요?

학습자는 측정값을 일괄 삽입하고 시간 범위 및 메타데이터 필드에 필터를 적용해 쿼리합니다. 브라우저에서 직접 실행하는 실습 코드로 MongoDB Academy을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

MongoDB Academy을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 MongoDB Academy은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 2번째 강의입니다.

“시계열 데이터 삽입 및 쿼리하기” 강의는 얼마나 걸리나요?

대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.

이 MongoDB Academy 강의에서 코드를 작성하고 실행할 수 있나요?

네. 모든 MongoDB Academy 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.

이 강의의 모든 강의

  1. 시계열 컬렉션 만들기
  2. 시계열 데이터 삽입 및 쿼리하기
  3. 시계열 데이터에 윈도 집계 적용하기
  4. expireAfterSeconds로 데이터 자동 만료
← MongoDB Academy(으)로 돌아가기