Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
2 tasks done
Systerr opened this issue Oct 24, 2024 · 1 comment · Fixed by #15035
Closed
2 tasks done

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

Systerr opened this issue Oct 24, 2024 · 1 comment · Fixed by #15035
Milestone

Comments

@Systerr
Copy link

Systerr commented Oct 24, 2024

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

@vkarpov15
Copy link
Collaborator

Here's a couple of options:

  1. Make syncIndexes() ignore time series indexes
  2. Try to compare the time series index to the timeseries option in the schema, and see if they match

We would need to implement a function to check if the specified Mongoose timeseries index matches the time series index returned from MongoDB

@vkarpov15 vkarpov15 modified the milestones: 8.8.1, 8.8.2 Nov 7, 2024
vkarpov15 added a commit that referenced this issue Nov 13, 2024
fix(model): make diffIndexes() avoid trying to drop default timeseries collection index
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants