Mongoose: Mixed Schema Type does not store Empty Objects.

Created on 23 Oct 2017  路  5Comments  路  Source: Automattic/mongoose

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

What is the current behavior?
Mixed Schema Type does not store Empty Objects.

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

Here is my Model

const GadgetPositionSchema = new Schema(
  {
    displayScheme: String,
    position: Schema.Types.Mixed,
    updatedOn: Date
  },
  { retainKeyOrder: true }
);

let gadgetPositionModel;

try {
  gadgetPositionModel = mongoose.model('gadgetpositions');
} catch (error) {
  gadgetPositionModel = mongoose.model('gadgetpositions', GadgetPositionSchema);
}

module.exports = gadgetPositionModel;

Here is the save query I run

const gadgetPositionObj = new gadgetPositionModel(); gadgetPositionObj.displayScheme = 'default'; gadgetPositionObj.position = {}; gadgetPositionObj.updatedOn = new Date(); return gadgetPositionObj.save();

What is the expected behavior?
It should save the document as

{
    "_id" : ObjectId("59eda54eb0202d00017820d7"),
    "displayScheme" : "default",
    "position": {}
    "updatedOn" : ISODate("2017-10-23T08:16:14.970Z"),
    "__v" : 0
}

What is the actual behavior?
Currently it saves the document as

{
    "_id" : ObjectId("59eda54eb0202d00017820d7"),
    "displayScheme" : "default",
    "updatedOn" : ISODate("2017-10-23T08:16:14.970Z"),
    "__v" : 0
}

Please mention your node.js, mongoose and MongoDB version.
Node: v6.10.3
MongoDB: v3.4.6
"mongoose": "^4.12.1",

won't fix

Most helpful comment

Currently by design, set the minimize option to false to turn this behavior off.

const GadgetPositionSchema = new mongoose.Schema(
  {
    displayScheme: String,
    position: mongoose.Schema.Types.Mixed,
    updatedOn: Date
  },
  { retainKeyOrder: true, minimize: false }
);

All 5 comments

Thanks for the repro instructions, will investigate and fix asap.

Currently by design, set the minimize option to false to turn this behavior off.

const GadgetPositionSchema = new mongoose.Schema(
  {
    displayScheme: String,
    position: mongoose.Schema.Types.Mixed,
    updatedOn: Date
  },
  { retainKeyOrder: true, minimize: false }
);

Might need to fix the docs then, @vkarpov15, because the docs say that mongoose removed empty object from "Schemas". However, my Schema doesn't contain an empty object but the object(model instance) I'm trying to save does.

Can you point me to the docs you're referring to @AyushG3112 ?

Currently by design, set the minimize option to false to turn this behavior off.

const GadgetPositionSchema = new mongoose.Schema(
  {
    displayScheme: String,
    position: mongoose.Schema.Types.Mixed,
    updatedOn: Date
  },
  { retainKeyOrder: true, minimize: false }
);

Thank you for this. It helped me solve an issue on the similar lines.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

simonxca picture simonxca  路  3Comments

jeremyml picture jeremyml  路  3Comments

adamreisnz picture adamreisnz  路  3Comments

lukasz-zak picture lukasz-zak  路  3Comments

p3x-robot picture p3x-robot  路  3Comments