Automattic / Automattic/mongoose

Add option to queue operations until `autoCreate` and `autoIndex` finish

Open
#11,916 4 comments 0 reactions 1 assignee Claimed by @vkarpov15 View on GitHub
enhancement
Dominant language
JavaScript
Stars
27.5k
Forks
4k
Avg merge
2d 7h
Merged PRs (30d)
35

Description

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

Here's the code

```typescript
// 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>(
'ActiveUser',
ActiveUserSchema,
'active_user',
);

```

image

_Originally posted by @satasuk03 in https://github.com/Automattic/mongoose/issues/10611#issuecomment-1150794186_

Note:
Mongoose Version: 6.3.6
Node Version: 16.13.1
MongoDB Version: 5.0.9

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.