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

Using MongooseMap causes memory leak on model.schema.subpaths #13874

Closed
2 tasks done
mistyByme opened this issue Sep 18, 2023 · 0 comments
Closed
2 tasks done

Using MongooseMap causes memory leak on model.schema.subpaths #13874

mistyByme opened this issue Sep 18, 2023 · 0 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@mistyByme
Copy link

mistyByme commented Sep 18, 2023

Prerequisites

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

Mongoose version

7.5.0

Node.js version

18.16.0

MongoDB server version

5.0

Typescript version (if applicable)

4.9.4

Description

model fields for model.schema.subpaths will collect the paht of map childSchema ,it will make the memory bigger and bigger if path changed.

Steps to Reproduce

import mongoose from 'mongoose';

mongoose.connect('mongodb://localhost:27017', { dbName: 'test' });
const tradeSchema = new mongoose.Schema({ tradeId: Number, content: String });

const testSchema = new mongoose.Schema(
    {
        userId: Number,
        tradeMap: {
            type: Map,
            of: tradeSchema,
        },
    },
);

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

main();

async function test() {
    const userId = 100;
    const user = await TestModel.create({ userId, tradeMap: new Map() });

    //add subDoc
    for (let id = 1; id <= 10; id++) {
        const trade = { tradeId: id, content: 'test' };
        user.tradeMap.set(trade.tradeId.toString(), trade);
    }
    await user.save();
    await TestModel.deleteOne({ userId });
}

async function main() {
    await test();
    //print TestModel.schema.subpaths
    console.log((TestModel.schema as any).subpaths);
}
    /* output
    {
  'tradeMap.1._id': undefined,
  'tradeMap.2._id': undefined,
  'tradeMap.3._id': undefined,
  'tradeMap.4._id': undefined,
  'tradeMap.5._id': undefined,
  'tradeMap.6._id': undefined,
  'tradeMap.7._id': undefined,
  'tradeMap.8._id': undefined,
  'tradeMap.9._id': undefined,
  'tradeMap.10._id': undefined,
  'tradeMap.1.tradeId': undefined,
  'tradeMap.1.content': undefined,
  'tradeMap.2.tradeId': undefined,
  'tradeMap.2.content': undefined,
  'tradeMap.3.tradeId': undefined,
  'tradeMap.3.content': undefined,
  'tradeMap.6.content': undefined,
  'tradeMap.7.tradeId': undefined,
  'tradeMap.7.content': undefined,
  'tradeMap.8.tradeId': undefined,
  'tradeMap.8.content': undefined,
  'tradeMap.9.tradeId': undefined,
  'tradeMap.9.content': undefined,
  'tradeMap.10.tradeId': undefined,
  'tradeMap.10.content': undefined
    }
     */

Expected Behavior

if the key of tradeMap is string for number type (user.tradeMap.key also equal to trade.id.toString()), it will made TestModel .schema.subpaths cache the child path (for exanmple ''tradeMap.6.content“、’tradeMap.8.tradeId” etc'). if trade.id keeps increasing, the memory of global variable TestModel will be increasing too.

Tasks

No tasks being tracked yet.
@mistyByme mistyByme changed the title mongoose model.schema.subpaths member leak for Map of childSchema mongoose model.schema.subpaths memory leak for Map of childSchema Sep 18, 2023
@mistyByme mistyByme changed the title mongoose model.schema.subpaths memory leak for Map of childSchema Using MongooseMap causes memory leak on model.schema.subpaths Sep 19, 2023
@vkarpov15 vkarpov15 added this to the 7.5.5 milestone Sep 26, 2023
@vkarpov15 vkarpov15 added the has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue label Sep 26, 2023
@vkarpov15 vkarpov15 added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed performance has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Oct 6, 2023
vkarpov15 added a commit that referenced this issue Oct 6, 2023
vkarpov15 added a commit that referenced this issue Oct 8, 2023
vkarpov15 added a commit that referenced this issue Oct 10, 2023
Avoid storing a separate entry in schema `subpaths` for every element in an array
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

No branches or pull requests

3 participants