-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
When calling lean() with transform on a document with embedded schema, the transform is also run on every level of embedded documents instead of just the parent document #13757
Comments
This is currently expected behavior: on |
On second thought, if you just want your transform logic to run on the top level document, you can do the following: const findStoreResult = await Store.findOne({ location: 'Tashbaan' })
.lean()
.exec();
findStoreResult.transformed = true;
|
@vkarpov15 Of course, I could set the transform logic inside To your point on handling recursion on subdocuments, there are other (IMO more appropriate, design-wise) ways to transform subdocuments:
I think ideally, |
My suggestion wasn't to remove |
@vkarpov15 Understood. I recently found that mongoose also allows me to set the lean transform in the schema itself, and have the transform run by default whenever I call |
@sharang-lyric unfortunately I don't see a valid reason to support running the transform on just the document. The logic from this comment is a simpler alternative that doesn't require any extra options. I'm going to close this issue for now. |
Apologies in advance for the necropost here, but I'm very curious. In my own usage, nested subdocuments are typically something with a containment relationship to the parent; that's it, that's all. They don't look like the parent, and the parent logic doesn't apply to them; they're contained entities. It therefore seems a bit unusual that calling The reason I ask, is that this exists in the documentation for
I believe that in the absence of subdocuments, that's a valid statement, but in the presence of them, it becomes a trap for the unwary. I surmise that the recursive behavior is intended to support a use case of I want to delete |
Our reasoning is that it is much easier to just manipulate the return value of toObject() rather than using transform if you don't want the recursive behavior. The recursive behavior has its uses, like, as you mentioned, removing id properties. |
Fair enough; I suggest something more explicit in the documentation to that effect, i.e., when transforming an object, manipulating the return value of |
That's a reasonable suggestion, we'll do that. |
Prerequisites
Mongoose version
7.4.3
Node.js version
18.16.1
MongoDB server version
5.0
Typescript version (if applicable)
No response
Description
I have a model
Store
with an array propertyemployees
where each element is a nested schemaEmployeeSchema
.When calling
Store.findOne()
with a lean transform function, in addition to being called on the store object, it is also being called on:employees
array itselfemployees
arraySteps to Reproduce
Code:
Output:
Expected Behavior
Only the outer store object should have
transformed: true
Expected output:
The text was updated successfully, but these errors were encountered: