Why by default all fields in mongoose are not required? - node.js

It seems that the default value of the required attributes of each field in mongoose schema is false. So, if I save any documents, those not required fields are not written into DB at all. I was wondering why this is designed in this way, and how I can change the default value to be true for all fields.
Thanks,

Following the documentation required has nothing to do with writing into the database (especially if it is false). It switches the field validation on or off. Default is off.
There must be another reason why your fields are not persisted.

Related

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!
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.

Maximo readonly/persistent attribute issues

I have an non-persistent attribute (SITEID) on my WOCHANGE object that originates from the parent object, WORKORDER. For some particular reason, this attribute has a few problems that I've never really seen with other attributes before.
Based on various configurations I have tried in an attempt to remedy the issue, here are the main issues:
It doesn't trigger the WOCHANGE to save when changed.
In addition to the value not being saved, I can change the value on one record, go to another and the value persists on the different record.
The field is readonly unless I define it to have an inputmode of DEFAULT. This is odd to me, because not defining inputmode usually implies default behavior (NOT readonly).
Here are the definitions for the SITEID attribute on both the WORKORDER
and WOCHANGE objects.
SITEID also uses a TABLE domain belonging to the SITE table.
Are there any attribute rules being applied from other sources that I should be checking?
That workorder field class on there may not be desired and may be messing with things, like setting the field to read-only. Site Id is commonly a read-only field, especially when the record is no longer a new record. Because of that, the logic to make that field read-only could be buried deeper in the Maximo business logic than just that field class. You are working with a field that has a lot of special meaning in Maximo, you are likely going to stumble into many built-in business rules.
Since non-persistent fields are not saved in the database (they are in memory fields only), I don't believe they trigger the flag for a record to be saved. What would be saved? Nothing in the database (a save) is to be changed yet.
Your screenshot however shows the field as persistent. Is WOCHANGE a view? I can't recall and no longer have the resources to check.

Dynamics CRM is ignoring the default value for two-option field

Quick question.
I have a custom two-option field on an entity, with "Yes"/"No" as the values; "Yes" has the underlying value 1, while "No" has the underlying value 0. I've set the default value for this field to "Yes". However, when I create new entity records, the field always gets the value "No" (0 in the database). It seems to be ignoring the default value I've set. Why?
The field is not present on any of the entity forms, as it's only used in underlying plugin code. Should that matter?
Are you creating a new record for this entity using code that uses the strongly-typed objects? If so, when you create a "new" entity in code, I'm guessing the class itself is setting that field to "false" by default. I don't think those generated classes respect the default values in the metadata. I also think that all fields are submitted on a create when you use these generated classes. That means that your class is setting it to "no" by default and then on create, the system thinks that you explicitly set it to "no" so default values are not applied. I think you need to explicitly remove that attribute from the attribute collection of your entity before you create it. That way the system should respect the default value on create. Sorry for all the "I thinks" but I'm not in a place that I can test or verify all of this. :)

Set default to Required for a field in a model in Mongoose?

I'm using Mongoose as a NodeJS client to communicate with MongoDB.
When I was defining my Schema, I found that for most of my fields I'd like to put a "required" validator on it.
I want to check if there're property I can set so that the "validator" is auto put onto a field, when I'm not specifying
required:false
to that field.
Thanks a lot
you can use the 'pre' method : http://mongoosejs.com/docs/api.html#schema_Schema-pre
it allow you to define a function to use before an event.
so you can define a validator before saving and attach it to you schema.
I know it's not attach to a field like you wanted but i hope it can help you

How does `mongoose` handle adding documents that have FIELDS that are __NOT__ part of the schema?

I'm playing around with quick start guide for mongoose.
http://mongoosejs.com/docs/index.html
I assumed that it would throw an error when I saved a document with a field NOT defined in the schema. Instead, it created a new document in the collection but without the field. (Note: I realize mongodb itself is "schema-less" so each document in a collection can be completely different from each other.)
two questions
How does mongoose handle adding documents that have fields that are NOT part of the schema? It seems like it just ignore them, and if none of the fields map, will create an empty document just with an ObjectId.
And how do you get mongoose to warn you if a specific field of a document hasn't been added even though the document successfully saved?
(The question is - I believe - simple enough, so I didn't add code, but I definitely will if someone requests.)
Thanks.
Q: How does mongoose handle adding documents that have fields that are NOT part of the schema?
The strict option, (enabled by default), ensures that values passed to our model constructor that were not specified in our schema do not get saved to the db.
- mongoose docs
Q: How do you get mongoose to warn you if a specific field of a document hasn't been added even though the document successfully saved?
The strict option may also be set to "throw" which will cause errors
to be produced instead of dropping the bad data. - mongoose docs
...but if you absolutely require saving keys that aren't in the schema, then you have to handle this yourself. Two approaches I can think of are:
1. To save keys that aren't in the schema, you could set strict to false on a specific model instance or on a specific update. Then, you'd need to write some validation that (a) the values in the document conformed to your standards and (b) the document saved in the database matched the document you sent over.
2. You could see if the Mixed schema type could serve your needs instead of disabling the validations that come with strict. (Scroll down to 'usage notes' on that link, as the link to the 'Mixed' documentation seems broken for the moment.)
Mongoose lets you add "validator" and "pre" middleware that perform useful functions. For instance, you could specify the required attribute in your schema to indicate that a specific property must be set. You could also specify a validator that you can craft to throw an error if the associated property doesn't meet your specifications. You can also set up a Mongoose "pre" validator that examines the document and throws an Error if it finds fields that are outside of your schema. By having your middleware call next() (or not), you can control whether you proceed to the document save (or not).
This question/response on stackoverflow can help with figuring out whether or not an object has a property.

Resources