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

[Question] Is there a way to create MongoDB 5.0 Time Series Collections with Mongoose 6.0? #10611

Closed
antonvalletas opened this issue Aug 25, 2021 · 5 comments
Labels
new feature This change adds new functionality, like a new method or class
Milestone

Comments

@antonvalletas
Copy link

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.

  • Node v16.7.0
  • MongoDB 5.0.2
  • Mongoose 6.0

I was wondering if there is a way to mark a Schema in Mongoose as being a Time Series Collection as described in the MongoDB 5.0 documentation, in order to use this new functionality without having to create these new types of collections manually outside of Mongoose.

I checked the Mongoose 6.0.0 docs and couldn't any info regarding this matter. Any help is appreciated.

Cheers!

@vkarpov15
Copy link
Collaborator

Currently, you would need to create the collection manually and disable autoCreate in your schema.

const schema = new Schema({ /* schema def */ }, { autoCreate: false, autoIndex: false });

const TestModel = mongoose.model('Test', schema);

// Later
await TestModel.createCollection({
  timeseries: {
    timeField: "timestamp",
    metaField: "metadata",
    granularity: "hours"
  },
  expireAfterSeconds: 86400
});

@vkarpov15 vkarpov15 added this to the 6.1.0 milestone Aug 25, 2021
@antonvalletas
Copy link
Author

Thanks for the info, looking forward to having it integrated in Mongoose @vkarpov15 :)

@IslandRhythms IslandRhythms added the help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary label Aug 26, 2021
@acomito
Copy link

acomito commented Sep 7, 2021

@vkarpov15 I tried that but got an error:

BSON field 'create.timeseries' is an unknown field

edit: ahhh -- I didn't upgrade my mongoDb to 5.0, of course

@vkarpov15 vkarpov15 added new feature This change adds new functionality, like a new method or class and removed help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary labels Sep 20, 2021
@vkarpov15
Copy link
Collaborator

In v6.1.0, you'll be able to define timestamps as a schema option:

const schema = new Schema({ name: String, timestamp: Date, metadata: Object }, {
  timeseries: {
    timeField: 'timestamp',
    metaField: 'metadata',
    granularity: 'hours'
  },
  expireAfterSeconds: 86400
});

Assuming autoCreate is enabled, Mongoose will use the schema timeseries option when automatically creating the collection. Or, if you manually call Model.createCollection(), Mongoose will use the schema timeseries by default.

vkarpov15 added a commit that referenced this issue Nov 30, 2021
@satasuk03
Copy link

I have a problem when I switch database before creating timeseries.
it will creates a collection instead o f timeseries.

Here's the code

// db.ts
export const useDB = (guidId: string) =>
  mongoose.connection.useDb(guidId, {
    // ensures connections to the same databases are cached
    useCache: true,
    // remove event listeners from the main connection
    noListener: true,
  });

// =====================================

// schema file
import { useDB } from 'db';
import mongoose, { Schema } from 'mongoose';

export interface ActiveUserDoc {
  timestamp: Date;
  messages: Number;
  metadata: { discordId: string };
}

const ActiveUserSchema = new Schema(
  {
    timestamp: Date,
    messages: Number,
    metadata: {
      discordId: String,
    },
  },
  {
    timeseries: {
      timeField: 'timestamp',
      metaField: 'metadata',
      granularity: 'hours',
    },
    expireAfterSeconds: 60 * 24 * 7, // 7 days
  },
);

export const getModel = (guildId: string) =>
  useDB(guildId).model<ActiveUserDoc, mongoose.Model<ActiveUserDoc>>(
    'ActiveUser',
    ActiveUserSchema,
    'active_user',
  );

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature This change adds new functionality, like a new method or class
Projects
None yet
Development

No branches or pull requests

5 participants