-
-
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
Document.toObject() does not apply to subdocuments #14589
Labels
confirmed-bug
We've confirmed this is a bug in Mongoose and will fix it.
Comments
IslandRhythms
added
the
confirmed-bug
We've confirmed this is a bug in Mongoose and will fix it.
label
May 13, 2024
const mongoose = require('mongoose');
const assert = require('assert');
const schema = new mongoose.Schema({name: String, docArr: [{name: String}]});
const TestModel = mongoose.model('Test', schema);
const doc = new TestModel({name: 'test', docArr: [{name: 'test'}]});
// pass the transform as an inline option. Deletes `_id` property
// from both the top-level document and the subdocument.
const obj = doc.toObject({transform: deleteId});
assert.equal(obj._id, undefined);
assert.equal(obj.docArr[0]._id, undefined);
function deleteId(doc, ret, options) {
delete ret._id;
return ret;
} |
vkarpov15
added a commit
that referenced
this issue
May 17, 2024
fix(document): ensure `transform` function passed to `toObject()` opt…
This was referenced Jun 7, 2024
This was referenced Jun 10, 2024
Merged
This was referenced Jul 4, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Prerequisites
Mongoose version
8.3.4
Node.js version
20.12.2
MongoDB server version
7.0.2
Typescript version (if applicable)
5.4.5
Description
When calling
myDocument.toObject
with options, for example to remove the _id, the options do not seem to apply to subdocuments.The documentation specifies (https://mongoosejs.com/docs/api/document.html#Document.prototype.toObject())
It worked with 8.3.3 and seems broken in 8.3.4
Steps to Reproduce
The test fails in 8.3.4 with
Expected Behavior
Any transform function specified in
toObject
options also propagates to any subdocuments.Thanks
The text was updated successfully, but these errors were encountered: