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

Document.toObject() does not apply to subdocuments #14589

Closed
2 tasks done
AymericBebert opened this issue May 13, 2024 · 1 comment · Fixed by #14600
Closed
2 tasks done

Document.toObject() does not apply to subdocuments #14589

AymericBebert opened this issue May 13, 2024 · 1 comment · Fixed by #14600
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.

Comments

@AymericBebert
Copy link

AymericBebert commented May 13, 2024

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

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())

function deleteId(doc, ret, options) {
  delete ret._id;
  return ret;
}

const schema = 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 });
obj._id; // undefined
obj.docArr[0]._id; // undefined

It worked with 8.3.3 and seems broken in 8.3.4

Steps to Reproduce

it('subDocument toObject', () => {
    function deleteId(doc: any, ret: any, options: any) {
        delete ret._id;
        return ret;
    }

    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});
    expect(obj._id).toEqual(undefined);
    expect(obj.docArr[0]._id).toEqual(undefined);
});

The test fails in 8.3.4 with

Expected: undefined
Received: "6641e1fe609d9d566a8bc095"

Expected Behavior

Any transform function specified in toObject options also propagates to any subdocuments.

Thanks

@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label May 13, 2024
@IslandRhythms
Copy link
Collaborator

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 16, 2024
vkarpov15 added a commit that referenced this issue May 17, 2024
fix(document): ensure `transform` function passed to `toObject()` opt…
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants