The link is http://mongoosejs.com/docs/api.html#schema_string_SchemaString-trim
I'm beginner in mongoosejs. I just don't get it...
I saw this question How to update mongoose default string schema property trim? but don't understand why trim. Im creating my first schema today like a 'hello world'.
I saw this to https://stackoverflow.com/tags/trim/info ... but when i need to use it, i want to learn more about it. Im looking for an explanation for a beginner...
The trim() function is a string function of Node. js which is used to remove white spaces from the string. Syntax: string.trim() Parameter: This function does not accepts any parameter. Return Value: The function returns the string without white spaces.
Mongoose | save() Function The save() function is used to save the document to the database. Using this function, new documents can be added to the database.
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.
A Mongoose schema defines the structure of the document, default values, validators, etc., whereas a Mongoose model provides an interface to the database for creating, querying, updating, deleting records, etc.
It's basically there to ensure the strings you save through the schema are properly trimmed. If you add { type: String, trim: true }
to a field in your schema, then trying to save strings like " hello"
, or "hello "
, or " hello "
, would end up being saved as "hello"
in Mongo - i.e. white spaces will be removed from both sides of the string.
Using trim will help in removing the white spaces present (beginning and ending of the string) in the string that you want to save to the DB like
"ABC " , " ABC ",
will be saved in the form
"ABC"
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