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

Discriminators: Return derived model when issuing Find on base model #14593

Open
1 task done
TomMettam opened this issue May 14, 2024 · 2 comments
Open
1 task done

Discriminators: Return derived model when issuing Find on base model #14593

TomMettam opened this issue May 14, 2024 · 2 comments
Labels
can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity.

Comments

@TomMettam
Copy link

TomMettam commented May 14, 2024

Prerequisites

  • I have written a descriptive issue title

Mongoose version

8.3.4

Node.js version

v20.11.1

MongoDB version

5.0.16-r1::gentoo

Operating system

Linux

Operating system version (i.e. 20.04, 11.3, 10)

Gentoo

Issue

I have just finished working on updating an old codebase.

As part of this work, I migrated from the old and largely defunct mongoose-extend-schema to Mongoose Discriminators.

However, there's a key change in functionality which is causing a significant loss in performance.

const baseModelSchema = new mongoose.Schema({
    ...
});

const derivedModelSchema = new mongoose.Schema({
    ...
});

const baseModel = connection.model('BaseModel', baseModelSchema);
const derivedModel = baseModel.discriminator('DerivedModel', derivedModelSchema);

With mongoose-extend-schema, if I did:

const result = await baseModel.findOne({ ... });

and the discriminator matched derivedModel, it would give me an instance of derivedModel.

With discriminators, it always gives me an instance of baseModel which means that I have to manually convert it, which costs a bunch of additional CPU cycles...

baseModelSchema.post('find', function (docs, next)
    {
        if (docs?.length)
        {
            for (let index = 0; index < docs.length; index++)
            {
                const doc = docs[index];
                const Model = connection.model(doc.__type);
                docs[index] = new Model(doc.toObject());
            }
        }
        next();
    });

This is suboptimal and slow, and I'd really like it if mongoose could give me the correct instance for the model. Is there an option to enable this?

@TomMettam TomMettam added help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary help wanted labels May 14, 2024
Copy link

This issue is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 5 days

@github-actions github-actions bot added the Stale label May 29, 2024
@vkarpov15
Copy link
Collaborator

I'm unable to repro, AFAIK Mongoose does give you the correct discriminator model with findOne(). For example, consider the following script. Can you please modify the following repro script to demonstrate the issue you're seeing?

'use strict';

const mongoose = require('mongoose');

run().catch(error => {
  console.error(error);
  process.exit(-1);
});

async function run() {
  await mongoose.connect('mongodb://127.0.0.1:27017/mongoose_test');

  const baseModelSchema = new mongoose.Schema({ baseModelProp: String });

  const derivedModelSchema = new mongoose.Schema({ derivedModelProp: String });

  const baseModel = mongoose.model('BaseModel', baseModelSchema);
  const derivedModel = baseModel.discriminator('DerivedModel', derivedModelSchema);

  await baseModel.deleteMany({});
  const { _id } = await derivedModel.create({ baseModelProp: 'base model test', derivedModelProp: 'derived model test' });

  const doc = await baseModel.findById(_id).orFail();
  console.log(doc.derivedModelProp); // Prints "derived model test"
  console.log(doc instanceof derivedModel); // Prints "true"
}

@vkarpov15 vkarpov15 added can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. and removed help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary help wanted Stale labels Jun 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity.
Projects
None yet
Development

No branches or pull requests

2 participants