Mongoose does not return properties that are empty objects

Viewed 267

When I fetch records from MongoDB, fetched documents does not have properties that exist in database but that are empty objects. Is there some config that I should add to fetch also properties that are empty objects?

1 Answers

You need to set minimize to false.

Official example:

const schema = new Schema({ name: String, inventory: {} }, { minimize: false });
const Character = mongoose.model('Character', schema);

// will store `inventory` if empty
const sam = new Character({ name: 'Sam', inventory: {} });
await sam.save();
doc = await Character.findOne({ name: 'Sam' }).lean();
doc.inventory; // {}
Related