7

I have my schema:

Schema = {
 name: String,
 email: String,
 animal: String
};

And i know that mongoose have some methods to help me uppercase, lowercase, even trim my String, but what about capitalize? I want be me able to uppercase only the firsts letters of the name and the email.

How can i do that??

I am using a form to catch the data, them save in my database using a post route, and there are some users that type all lowecase, and i was trying deal with this problem with css.

input#name {
 text-transform: capitalize;
}

But this not work.

Community
  • 1
  • 1
  • Do you need to handle multiple languages and if so, which ones? – unobf Jan 23 '15 at 18:41
  • i am using javascript for pretty much everything @unobf –  Jan 23 '15 at 18:43
  • No but I mean, do you need to support Chinese, Japanese, German etc.? – unobf Jan 23 '15 at 18:45
  • If you uppercase the first letter of emails, you've generally changed the email. If you uppercase the first letter of names, you're changing peoples names, not all names start with an uppercase letter, and this is generally not something you should do ? – adeneo Jan 23 '15 at 18:46
  • 1
    oh sorry, i need support for portuguese, but this is revelant? strings are just strings.. no? –  Jan 23 '15 at 18:47
  • here in my country every name starts with a uppercase letter, without exception, is a rule. –  Jan 23 '15 at 18:47
  • i agree with u, for emails i need set just to lowecase, but in the name, i need capitalize the word. –  Jan 23 '15 at 18:48
  • 1
    So you have no immigrants, say someone named al-Hussein, or with last names like deGeneres etc ? – adeneo Jan 23 '15 at 18:48
  • 1
    And this is now a duplicate of [**this**](http://stackoverflow.com/questions/1026069/capitalize-the-first-letter-of-string-in-javascript), and the answer below is a direct copy/paste – adeneo Jan 23 '15 at 18:53
  • this site is just for brazilians @adeneo, and of course some immigrant who speaks Portuguese can use it, but i want capitalize the word just for search throw the database. –  Jan 23 '15 at 18:54

6 Answers6

18

Best way is to use Mongooses Baked in functionality - this should do it!

Schema = {
 name:{
    type: String,
    uppercase: true
 },
 email: String,
 animal: String
};
fino
  • 3,764
  • 2
  • 23
  • 16
6

CSS Styles are only on the visible side, not on the data side.

You have to use Javascript to do this:

schema.pre('save', function (next) {
  // capitalize
  this.name.charAt(0).toUpperCase() + this.name.slice(1);
  next();
});

Edit: As Luis Febro mentioned in the comments underneath, the current implementation keeps the upper/lowercase spelling of the rest of the string. If you really want to make sure, that there is only the first letter capitalized and the rest is built out of lowercase letters, you could adjust the code like this:

this.name.charAt(0).toUpperCase() + this.name.slice(1).toLowerCase()
schlenger
  • 1,447
  • 1
  • 19
  • 40
  • i want capitalize, not lowercase @schlenger –  Jan 23 '15 at 18:43
  • sorry, read the wrong thing. Should be the right solution now. – schlenger Jan 23 '15 at 18:49
  • yesterday i found the schema.pre, but i didnt not that i can do that.. thats why i love js, i can do everything! haha.. Thanks schlenger, i will accept your answer in 5 min. –  Jan 23 '15 at 18:50
  • 1
    if the user types like: hELEN, this callback returns all letters in uppercase. This should work in all situations: `this.name.charAt(0).toUpperCase() + this.name.slice(1).toLowerCase()` – Luis Febro Nov 17 '19 at 04:20
  • 1
    @LuisFebro Yes, you're right. However, you may want to keep the existing uppercase letter of the input string. Nevertheless, this is a nice additional input. I'll edit the answer – schlenger Nov 26 '19 at 10:01
1

Best practices

schema.pre("save", function(next) {
  this.name =
    this.name.trim()[0].toUpperCase() + this.name.slice(1).toLowerCase();
    next();
});
Prateek Arora
  • 616
  • 8
  • 7
  • Hi! This seems to be the same as the highest-voted existing answer. What are you recommending that is different? Can you explain why that answer isn't best? – DavidW Sep 11 '19 at 20:16
  • What happened to the mongoose built-in trim? How is this the best practice? – echelon Dec 08 '21 at 18:41
1

For capitalize all the words in the string you can try this...

personSchema.pre('save', function (next) {
  const words = this.name.split(' ')
  this.name = words
    .map((w) => w.charAt(0).toUpperCase() + w.slice(1).toLowerCase())
    .join(' ')
  next()
})

Useful if you have a compound name... 'john doe' => 'John Doe'

-1

When you're outputting the name in JavaScript you could create a new string with the name capitalized.

var capName = user.name[0].toUpperCase() + user.name.slice(1);

This will capitalize the first letter and combine it with the remaining letters of the string to capitalize the word and save it in a new variable.

Russell Ingram
  • 114
  • 1
  • 1
  • 11
-2

from mongoose doc here under the String sub-section you will find all functions that could e applied to you schema option.

Schema = {
 email: {type: String, lowercase: true, trim: true},
 animal: {type: String}
};
kheengz
  • 840
  • 12
  • 10