Mongoose schema creates an empty array field

In my schema Food, I have different fields based on another field called yummy. For instance:

  • If yummyequal pancake, my fields doughandjam
  • If yummyequal pizza, my fields dough, meatandcheese

etc .. It works as expected when saving documents - I just pass in the fields that I want to save in the document, since none of them are required, except yummy. Therefore, if I save:

{
   yummy: 'pizza'
   dough: 'thin'
   meat : 'peperroni',
   cheese: [ObjectId("59a65a4f2170d90b0436ed4b")] // refs to the cheeses collection.
}

My document looks as expected.

The problem is the field type: Array. When I save:

{
    yummy: 'pancake',
    dough: 'thick',
    jam: 'blueberry'
}

My document is saved with an extra field cheese: []. In my scheme, cheese is defined as follows:

Mongoose.model('Food', {
    ...
    cheese: { 
        type: [{ type: Mongoose.Schema.ObjectId, ref: 'Cheese' }], 
        required: false 
    },
    ...
});

, mongo , , $push, , - . , : ?

+4
1

, mongoose .

, , . , .

.


, ?

, . . , cheese, .


.

,

+5

Source: https://habr.com/ru/post/1684725/


All Articles