Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the meaning of 'trim' when use in mongoose?

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

like image 961
Undefined Behavior Avatar asked Dec 24 '13 20:12

Undefined Behavior


People also ask

What is trim in node JS?

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.

What is Save () in Mongoose?

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.

What is __ V 0 in Mongoose?

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.

Why schema is used in Mongoose?

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.


2 Answers

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.

like image 104
Traveling Tech Guy Avatar answered Oct 13 '22 18:10

Traveling Tech Guy


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" 
like image 37
Sachin Avatar answered Oct 13 '22 19:10

Sachin