Skip to content

syncIndexes (diffIndexes) and time series mongo system index #14984

@Systerr

Description

@Systerr

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

8.7.2

Node.js version

22.0

MongoDB server version

7.0

Typescript version (if applicable)

No response

Description

Time Series a little bit tricky on MongoDB. They are "MongoDB automatically creates a compound index on both the metaField and timeField of a time series collection." https://www.mongodb.com/docs/manual/core/timeseries-collections/#indexes
Mongoose diffIndexes have no aware of it and propose to delete this index. I suppose it is really expected behaviour as mongoose sync indexes between schema and database (with _id index exception)

Steps to Reproduce

  const schema = new mongoose.Schema(
    {
      time: {
        type: Date,
      },
      deviceId: {
        type: String,
      },
    },
    {
      timeseries: {
        timeField: 'time',
        metaField: 'deviceId',
        granularity: 'seconds',
      },
      autoCreate: false,
    },
  );

const model  = mongoose.model(
   'TimeSeriesTest'
    schema,
  );

This will make an index on mongoDB

// name deviceId_1_time_1
{ deviceId:1, time:1 }

Now

model.diffIndexes()
// TimeSeriesTest { toDrop: [ 'deviceId_1_time_1' ], toCreate: [] }

Expected Behavior

In ideal scenario Mongoose should understand time series index and skip it. But this index can be changes from version to version (looks like it introduced from mongo v6.3, but time series from mongo v5 https://www.mongodb.com/docs/manual/core/timeseries-collections/#benefits).

At least will be good to add note to documentation and/or warn about time series collection and required flag to work on it

But in a general MongoDB can introduce auto indexes for other collection too and somehow mongoose should understand it

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions