You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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?
The text was updated successfully, but these errors were encountered:
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';constmongoose=require('mongoose');run().catch(error=>{console.error(error);process.exit(-1);});asyncfunctionrun(){awaitmongoose.connect('mongodb://127.0.0.1:27017/mongoose_test');constbaseModelSchema=newmongoose.Schema({baseModelProp: String});constderivedModelSchema=newmongoose.Schema({derivedModelProp: String});constbaseModel=mongoose.model('BaseModel',baseModelSchema);constderivedModel=baseModel.discriminator('DerivedModel',derivedModelSchema);awaitbaseModel.deleteMany({});const{ _id }=awaitderivedModel.create({baseModelProp: 'base model test',derivedModelProp: 'derived model test'});constdoc=awaitbaseModel.findById(_id).orFail();console.log(doc.derivedModelProp);// Prints "derived model test"console.log(docinstanceofderivedModel);// Prints "true"}
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
Prerequisites
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.
With
mongoose-extend-schema
, if I did:const result = await baseModel.findOne({ ... });
and the discriminator matched
derivedModel
, it would give me an instance ofderivedModel
.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...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?
The text was updated successfully, but these errors were encountered: