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

Empty nested schemas are not minimized #11247

Closed
Aloune92 opened this issue Jan 19, 2022 · 1 comment
Closed

Empty nested schemas are not minimized #11247

Aloune92 opened this issue Jan 19, 2022 · 1 comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@Aloune92
Copy link

Aloune92 commented Jan 19, 2022

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

What is the current behavior?
Empty nested schemas are not minimized.

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

const { model, Schema } = require('mongoose');

const nestedSchema = new Schema({
  bar: String
}, { _id: false });

const schema = new Schema({
  foo: nestedSchema
});

const MyModel = model('MyModel', schema);

const myModel = await MyModel.create({ foo: {} });

console.log(myModel); // { _id: 61e82a9247bc4b3b51231301, foo: {}, __v: 0 }

What is the expected behavior?
foo is stored in db . It should not and myModel.foo should not appears in the result of the console.log.

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

Node v16.10.0
Mongoose v5.13.9 or v6.1.7
MongoDB v4.4.7

@Aloune92 Aloune92 changed the title Empty nested schemas are not minimized after set the nested schema Empty nested schemas are not minimized if empty Jan 19, 2022
@Aloune92 Aloune92 changed the title Empty nested schemas are not minimized if empty Empty nested schemas are not minimized Jan 19, 2022
@Aloune92 Aloune92 reopened this Jan 19, 2022
@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Jan 19, 2022
@IslandRhythms
Copy link
Collaborator

const mongoose = require('mongoose');

const nestedSchema = new mongoose.Schema({
  bar: String
}, { _id: false });

const schema = new mongoose.Schema({
  foo: nestedSchema
});

const MyModel = mongoose.model('MyModel', schema);


async function run() {
    await mongoose.connect('mongodb://localhost:27017/test');
    await mongoose.connection.dropDatabase();
    const myModel = await MyModel.create({ foo: {} });

    console.log(myModel); // { _id: 61e82a9247bc4b3b51231301, foo: {}, __v: 0 }
}

run();

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