2

In my Food schema, I have different fields based on the other field called yummy. For example:

  • if yummy equals pancake, my fields are dough and jam
  • if yummy equals pizza, my fields are dough, meat and cheese

etc. It works as expected upon saving docs - I just pass the fields I want to be saved per the document, as none of them are required except yummy. So if I save:

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

My document looks like expected.

The problem is with field of type: Array. When I save:

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

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

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

I checked if mongo needs the doc to have an array field predefined as empty in case of $push used in update - it does not. So, the question is: how do I avoid adding an empty array field to my document upon saving?

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
wscourge
  • 10,657
  • 14
  • 59
  • 80
  • 1
    It depends on your schema, if your schema have an array defined in it, it will create an empty array tho at the document creation. If you want to play with two version of the collection (one with cheese, one without), create two version of the schema [look here](https://stackoverflow.com/questions/14453864/use-more-than-one-schema-per-collection-on-mongodb) – Orelsanpls Aug 30 '17 at 07:19
  • So did you found something? I'm actually interested – Orelsanpls Aug 31 '17 at 14:53
  • 1
    I did as you suggested - played with two version of the collection and created separated schemas for `pizza` and `pancake` – wscourge Aug 31 '17 at 20:30
  • You should put what you did as answer and accept it, it can actually help some ppl after you :) Good job – Orelsanpls Sep 01 '17 at 07:31
  • 1
    Or, you should do that, it's your answer really. – wscourge Sep 01 '17 at 08:14

2 Answers2

5

When you are saving the document, mongoose look at the schema of the related collection.

It takes all fields you are giving to it and set the values up. It also create default values for the fields you did provide nothing for.

That's the behavior of mongoose.


In your case what can we do?

We gonna create two schema related to the same collection. One with the field cheese and one without. So when you gonna save a new document with the schema without cheese, it's not gonna get created.


In practice, the collection will hold two different document type.

Here is a link about using multiple schema related to a single collection

Orelsanpls
  • 22,456
  • 6
  • 42
  • 69
2

Just set default: undefined to field cheese. According to this.

...
cheese: { 
   type: [{ type: Mongoose.Schema.ObjectId, ref: 'Cheese' }], 
   required: false,
   default: undefined
},
...
fsevenm
  • 791
  • 8
  • 19