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

6

45

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

Insensitive answered 24/12, 2013 at 20:19 Comment(0)
E
122

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.

Electrokinetic answered 24/12, 2013 at 20:23 Comment(5)
@bruno.karklis Read documents about javascript trim function.Richellericher
@xpto Why this behave like that as normal javascript function won't behave the same <script> function myFunction() { var str = " Hello World! "; alert(str.trim()); } </script> <!-- This will display "Hello World!"Waxwork
@VIKASKOHLI because in that time i dint know the JS syntax well, i was learning, my background was SQL and PHP, and the thinking process is diferent, you dont use php syntax in SQL databade to do something. Besides that i knew that the database was made in JS, i was not aware that i could use JS syntax in the database.Insensitive
another question is that "should we really handle such things at DB level?" since it can be handled through server code. Are there any performance benefits?Unaffected
@UndefinedBehavior It you will add " hello world", its will be "hello world"Erikaerikson
S
3

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"
Saleable answered 5/1, 2020 at 11:41 Comment(0)
B
2
trim: true

will remove leading and trailing whitespaces so something like

" hello "

will be saved as

"hello"

Begat answered 2/12, 2022 at 18:50 Comment(0)
E
0

trim in Mongoose removes the space if it is at the beginning or end of the string ,

exemple : ' hello world '

when you activate trim in attribute It is saved : 'hello world'

Erb answered 25/10, 2023 at 15:47 Comment(0)
D
-2

trim in mongoose use to remove the white spaces from the strings

Depurative answered 8/4, 2020 at 9:6 Comment(1)
Trim removes whitespace characters from the beginning and end of a string.Accredit
A
-2

Actually, we use trim in mongoose to remove white space in a string.

For example: without use trim or trim:false

"mong oose "

with use trim or trim:true

"mongoose"

Note: gain better experience to read mongoose doc.

Aplacental answered 14/4, 2023 at 4:9 Comment(1)
As said in documentation: JavaScript strings have a trim() method that you can use to can use to remove leading and trailing whitespace from a string. Trim don't remove white space in the middle of words. so trimmed "mong oose " is "mong oose". Please check and fixAccredit

© 2022 - 2025 — McMap. All rights reserved.