-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
Issues when migrating from 5.12.3 to 6.8.0 #12806
Comments
I'm not sure I understand your question. The docs are very clear
Are you requesting a feature to disable |
Well, kind of, part of my second question is if if there is a way to disable (all that of course is only relevant to the second question and not the first) |
Currently no.
Why what is the case? Not having the option to disable _id for subdocs globally or having to set |
as a note, you can globally disable // "as any" casting is currently required because "defaultOptions" does not exist in the mongoose types
(mongoose.Schema.Types.Subdocument as any).defaultOptions = { ...(mongoose.Schema.Types.Subdocument as any).defaultOptions, _id: false }; put the above code before creating / importing any schemas
this error is probably because you are trying to use property the best solution would probably to be to set the schema type with btw, it is not recommended to extend from personally i recommend using a simple wrapper type (mostly copied from typegoose): type DocumentType<T, QueryHelpers = {}> = (T extends {
_id: unknown;
} ? mongoose.Document<T['_id'], QueryHelpers, T> : mongoose.Document<any, QueryHelpers, T>) & T;
// which can be used like
type PaymentDocument = DocumentType<PaymentInterface>;
type BankPaymentDocument = DocumentType<BankPaymentInterface>;
type CashPaymentDocument = DocumentType<CashPaymentInterface>; |
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 |
In the following code: bankPaymentSchema.virtual("displayOwner").get(function (): string {
return this.owner.toString();
}); in Mongoose 5.12.3, Here's a couple of workarounds:
const bankPaymentSchema = new Schema<PaymentInterface & BankPaymentInterface>(
{
bankReference: { type: String, required: true },
}
);
bankPaymentSchema.virtual<BankPaymentDocument>("displayOwner").get(function (): string {
return this.owner.toString();
}); |
docs(typescript): add notes about virtual context to Mongoose 6 migration and TypeScript virtuals docs
Prerequisites
Mongoose version
6.8.0
Node.js version
16.10.0
MongoDB version
5.0.14
Operating system
None
Operating system version (i.e. 20.04, 11.3, 10)
No response
Issue
I've completed the migration of my code from using Mongoose 5.12.3 to using 6.8.0.
I've followed the guidelines in this guide, and everything has worked as expected except from a couple of things:
Usage of
this
in virtual functions when also using discriminators.I have a main schema that contains some basic values and two schemas that inherit from it. Let's say, for the sake of this issue, that I have a
Payments
collection, and two types of Payments that inherit from the mainPayment
schema,BankPayment
andCashPayment
.Up until this point, I would structure this as follows:
And up until this point, I could implement virtual methods such as this:
However, when we migrated to 6.8.0, this started complaining with the following error:
And it seems like I am forced to cast the above like so:
But even this fails with:
I am trying to understand what
this
is in the context of a virtual method implementation such as the above and how I can work around this.Population of
_id
in all subdocumentsIt seems like from the moment I migrated to 6.8.0, I have to specify
_id: false
in all theobject
types in my schemas. Is there a way to default to this behaviour? I don't have any use cases I'd want a_id
field in an object in my schema.The text was updated successfully, but these errors were encountered: