Mike
Mike

Reputation: 33

Sometimes I have a number, sometimes I have nothing. Is the `Number` type appropriate in the Schema?

There's a field in my database where sometimes I want to store a number. However, sometimes I don't have a number to store (or I need to delete the number that was previously stored), so I want to store nothing (an empty field).

How should I store it? As undefined, as null or as NaN?

And is the Number type correct for my use? Or should I choose Mixed?

Thanks all!

Upvotes: 0

Views: 20

Answers (1)

Mika Sundland
Mika Sundland

Reputation: 18939

You can use Number as type and not set required: true. Or explicitly set it false. That will allow you to use both null, undefined and simply not having that field in the document.

Whether you want to store the undefined number as null or not having the field is up to you. You could also use both: don't have it defined in the document if it's not provided during creation, and using null when the number is not provided during update of the document, i.e. the number was removed at a later stage. That way you can tell if the number was there earlier if you ever encounter null.

Upvotes: 1

Related Questions