Mongoose seems to default to make all fields not required. Is there any way to make all the fields required without changing each of:
Dimension = mongoose.Schema(
name: String
value: String
)
to
Dimension = mongoose.Schema(
name:
type: String
required: true
value:
type: String
required: true
)
It'll get really ugly since I have a lot of these.
You can also set the default schema option to a function. Mongoose will execute that function and use the return value as the default.
The __v field is called the version key. It describes the internal revision of a document. This __v field is used to track the revisions of a document. By default, its value is zero ( __v:0 ).
save() is a method on a Mongoose document. The save() method is asynchronous, so it returns a promise that you can await on. When you create an instance of a Mongoose model using new, calling save() makes Mongoose insert a new document.
Definition. Removes whitespace characters, including null, or the specified characters from the beginning and end of a string. The string to trim.
I ended up doing this:
r_string =
type: String
required: true
r_number =
type: Number
required: true
and on for the other data types.
You could do something like:
var schema = {
name: { type: String},
value: { type: String}
};
var requiredAttrs = ['name', 'value'];
for (attr in requiredAttrs) { schema[attr].required = true; }
var Dimension = mongoose.schema(schema);
or for all attrs (using underscore, which is awesome):
var schema = {
name: { type: String},
value: { type: String}
};
_.each(_.keys(schema), function (attr) { schema[attr].required = true; });
var Dimension = mongoose.schema(schema);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With