Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

558
Views
How to create Time Series Collection with Mongoose

MongoDB v5.0 introduced a new collection type that is optimized for storing and working with time-series data - Time Series Collections. How can we create a Time Series Collection with Mongoose?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

It can be done with additional timeseries config property when defining the Schema.

const schema = Schema(
  { 
    property_1: String,
    property_2: String,
    timestamp_property: Date, 
    metadata_property: Object,
  {
    timeseries: {
      timeField: 'timestamp_property',
      metaField: 'metadata_property',
      granularity: 'hours',
    },
  }
);
  • timeField is required. It is the name of the field which contains the date in each time series document.

  • metaField is optional. It is the name of the field which contains metadata in each time series document. The metadata in the specified field should be data that is used to label a unique series of documents. The metadata should rarely, if ever, change.

  • granularity is optional. Possible values are "seconds", "minutes", and "hours". By default, MongoDB sets the granularity to "seconds" for high-frequency ingestion. Manually set the granularity parameter to improve performance by optimizing how data in the time series collection is stored internally. To select a value for granularity, choose the closest match to the time span between consecutive incoming measurements.


Example

Imagine you want to store Temperature records for every hour. In that case you would define your schema in the following way:

onst schema = Schema(
  {
    temperature: Number,
    calculated_at: Date,
    metadata: {
      sensorId: String,
      location: String,
    },
  },
  {
    timeseries: {
      timeField: 'calculated_at',
      metaField: 'metadata',
      granularity: 'hours',
    },
  }
);
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!