Automattic / mongoose

MongoDB object modeling designed to work in an asynchronous environment.
https://mongoosejs.com
MIT License
26.96k stars 3.84k forks source link

Empty nested schemas are not minimized #11247

Closed Aloune92 closed 2 years ago

Aloune92 commented 2 years ago

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

IslandRhythms commented 2 years ago
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();