0

I noticed a very strange behavior when creating a document

export const questionSchema: Schema<IQuestion> = new Schema<IQuestion>(
    {
        questionLevel: {
            type: Schema.Types.Number,
            index: true
        },
        answer: {
            type: Schema.Types.String
        },
        translations: [
            {
                language: {
                    type: Schema.Types.String
                },
                question: {
                    type: Schema.Types.String
                }
            }
        ]
    },
    { timestamps: true, collection: EDb.QUESTION_MODEL_NAME, minimize: false }
);

When I try to save a new document:

new this.model(newData).save()

The document is created but without an array

But if i change the code to

const newData: any = new this._model();
for (const key in data) {
    newData[key] = data[key];
}

return newData.save();

everything starts to work right

Ashot
  • 245
  • 6
  • 16

1 Answers1

0

Mongoose will not show an empty array. If your model schema has an array field but when you save the document, if you haven't added any element to that array, it won't show the array unless it has one element added to it.

Abhishek Bhagate
  • 5,583
  • 3
  • 15
  • 32