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

.toObject({flattenMaps: true}) on parent requires explicitly setting flattenMaps: true on child documents nested in maps #10653

Closed
TimUnderhay opened this issue Sep 1, 2021 · 1 comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@TimUnderhay
Copy link

Do you want to request a feature or report a bug?
Bug.

What is the current behavior?
When calling .toObject({flattenMaps: true}) on a parent document, maps of child documents are not flattened, unless one explicitly sets childSchema.set('toObject', { flattenMaps: true }); on every child schema.

If the current behavior is a bug, please provide the steps to reproduce.

const mongoose = require('mongoose');
const util = require('util');

const NestedChildSchema = new mongoose.Schema({
  myValue: {
    type: String
  }
}, {_id: false});

const ChildSchema = new mongoose.Schema({
  myChildMap: {
    type: Map,
    of: NestedChildSchema
  }
}, {_id: false});


const Schema = new mongoose.Schema({
  myMapSubdocs: {
    type: Map,
    of: ChildSchema,
    required: true
  }
}, {minimize: false, collection: 'test'});

const test = async () => {
  const nestedChild = {myValue: 'abc'};
  const child = {
    myChildMap: new Map().set('myChildMap', nestedChild)
  };
  const parent = {
    myMapSubdocs: new Map().set('myChild', child)
  };
  const BadModel = mongoose.model('BadModel', Schema);
  const badParentDoc = new BadModel(parent);

  const myBadFlattenedObject = badParentDoc.toObject({flattenMaps: true});
  console.log(`Doesn't flatten child schemas when they're part of maps:`)
  console.log(util.inspect(myBadFlattenedObject, {showHidden: false, depth: null}));
    
  console.log(`\n\nProperly flattens child schemas when they're part of maps.  The only difference is we set flattenMaps on ChildSchema:`)
  ChildSchema.set('toObject', { flattenMaps: true });
  const GoodModel = mongoose.model('GoodModel', Schema);
  const goodParentDoc = new GoodModel(parent);
  const goodFlattenedObject = goodParentDoc.toObject({flattenMaps: true});
  console.log(util.inspect(goodFlattenedObject, {showHidden: false, depth: null}));
};

const main = async () => {
  await test();
  process.exit(0);
};

main();

Output:

Doesn't flatten child schemas when they're part of maps:
{
  myMapSubdocs: {
    myChild: { myChildMap: Map(1) { 'myChildMap' => { myValue: 'abc' } } }
  },
  _id: new ObjectId("612f9f9ff10699bd21543208")
}


Properly flattens child schemas when they're part of maps.  The only difference is we set flattenMaps on ChildSchema:
{
  myMapSubdocs: {
    myChild: { myChildMap: { myChildMap: { myValue: 'abc' } } }
  },
  _id: new ObjectId("612f9f9ff10699bd21543209")
}

What is the expected behavior?
If I call .toObject({flattenMaps: true}) on a parent document, I would expect not to have to explicitly set flattenMaps: true on all child schemas which are part of maps in order to flatten them.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Mongoose 6.0.3
Node v14.17.6

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

I'm getting the same output

const mongoose = require('mongoose');
const util = require('util');

const NestedChildSchema = new mongoose.Schema({
  myValue: {
    type: String
  }
}, {_id: false});

const ChildSchema = new mongoose.Schema({
  myChildMap: {
    type: Map,
    of: NestedChildSchema
  }
}, {_id: false});


const Schema = new mongoose.Schema({
  myMapSubdocs: {
    type: Map,
    of: ChildSchema,
    required: true
  }
}, {minimize: false, collection: 'test'});

const test = async () => {
    await mongoose.connect('mongodb://localhost:27017/test', {
        useNewUrlParser: true,
        useUnifiedTopology: true,
      });
      await mongoose.connection.dropDatabase();
  const nestedChild = {myValue: 'abc'};
  const child = {
    myChildMap: new Map().set('myChildMap', nestedChild)
  };
  const parent = {
    myMapSubdocs: new Map().set('myChild', child)
  };
  const BadModel = mongoose.model('BadModel', Schema);
  const badParentDoc = new BadModel(parent);

  const myBadFlattenedObject = badParentDoc.toObject({flattenMaps: true});
  console.log(`Doesn't flatten child schemas when they're part of maps:`)
  console.log(util.inspect(myBadFlattenedObject, {showHidden: false, depth: null}));
    
  console.log(`\n\nProperly flattens child schemas when they're part of maps.  The only difference is we set flattenMaps on ChildSchema:`)
  ChildSchema.set('toObject', { flattenMaps: true });
  const GoodModel = mongoose.model('GoodModel', Schema);
  const goodParentDoc = new GoodModel(parent);
  const goodFlattenedObject = goodParentDoc.toObject({flattenMaps: true});
  console.log(util.inspect(goodFlattenedObject, {showHidden: false, depth: null}));
};

const main = async () => {
  await test();
  process.exit(0);
};

main();

@vkarpov15 vkarpov15 added this to the 6.0.5 milestone Sep 2, 2021
vkarpov15 added a commit that referenced this issue Sep 4, 2021
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

No branches or pull requests

3 participants