-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Comments
Currently, you would need to create the collection manually and disable 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
}); |
Thanks for the info, looking forward to having it integrated in Mongoose @vkarpov15 :) |
@vkarpov15 I tried that but got an error:
edit: ahhh -- I didn't upgrade my mongoDb to 5.0, of course |
In v6.1.0, you'll be able to define const schema = new Schema({ name: String, timestamp: Date, metadata: Object }, {
timeseries: {
timeField: 'timestamp',
metaField: 'metadata',
granularity: 'hours'
},
expireAfterSeconds: 86400
}); Assuming |
I have a problem when I switch database before creating 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',
); |
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
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!
The text was updated successfully, but these errors were encountered: