Syntax question (capitalize first letter)

Hello! I wanted to Capitalize the first letter only for all users. I found this snippet that works fine but only when the name is directly on the user while in our structure it’s one level down so profile.firstName I was hoping i could just use profile.firstName but that didn’t work. What do i need to add/change in order for the snippet to target the correct key? (sorry new to mongodb)

So in the example below firstName is actually profile.firstName :thinking:

db.hej.aggregate([
       {
          "$addFields": {
             "firstName": {
                "$reduce": {
                   "input": {
                      "$map": {
                         "input": {
                            "$split": [
                               "$firstName",
                               " "
                            ]
                         },
                         "in": {
                            "$concat": [
                               {
                                  "$toUpper": {
                                     "$substrCP": [
                                        "$$this",
                                        0,
                                        1
                                     ]
                                  }
                               },
                               {
                                  "$toLower": {
                                     "$substrCP": [
                                        "$$this",
                                        1,
                                        { "$strLenCP": "$$this" }
                                     ]
                                  }
                               }
                            ]
                         }
                      }
                   },
                   "initialValue": "",
                   "in": {
                      "$concat": [
                         "$$value",
                         " ",
                         "$$this"
                      ]
                   }
                }
             }
          }
       }
    ]
)

Hello @David_N_A7 , Welcome to the MongoDB community forum,

Can you please provide an example document structure and the expected result?

@turivishal Looks like:

{
    "_id": {
      "$oid": "637e1662d8095ee22f03b82f"
    },
    "profile": {
      "firstName": "john",
      "lastName": "Smith"
    },
    "age": 12
  },

Expected rresult after:

{
    "_id": {
      "$oid": "637e1662d8095ee22f03b82f"
    },
    "profile": {
      "firstName": "John",
      "lastName": "Smith"
    },
    "age": 12
  },

The properties firstName and lastName are inside a profile object so don’t need to do any $reduce or $map operators, look at the below pipeline, it will cut the first character from the string and make it upper case and concat with next characters,

db.hej.aggregate([
  {
    $set: {
      "profile.firstName": {
        $concat: [
          { $toUpper: { $substr: ["$profile.firstName", 0, 1] } },
          { $substr: ["$profile.firstName", 1, { $strLenCP: "$profile.firstName" }] }
        ]
      },
      "profile.lastName": {
        $concat: [
          { $toUpper: { $substr: ["$profile.lastName", 0, 1] } },
          { $substr: ["$profile.lastName", 1, { $strLenCP: "$profile.lastName" }] }
        ]
      }
    }
  }
])

Hi @turivishal thank you! However it doesn’t seem to work? Nothing happens at all when i run the code?

As per your screenshot, you have added a find() query at the end and I think that replaces the aggregate() query result, can you make sure after removing that find() command?

I don’t know more about this editor so try to execute in MongoDB shell or MongoDB compass.

I am clearing the thing up, this aggregation query will format the result only not the actual document in the database, if you want to update permanently in the database then you have to use update commands with update with aggregation pipeline.

Yeah was supposed to be permanent overwritten. And its only something i´m gonna run once. All this code is way above my head as a complete beginner hehe… The first code i posted did exactly everything i wanted and needed, only problem was that it only worked if the firstname was direct in root and not as a sub document inside “profile” so my problem was how to write the syntax so it targets firstname inside of profile.

The query will be same that i have suggested above, you just need to use .updateMany() method instead of .aggregate().

Mhm I get an error sadly :confused:

I forgot to mention, just add the query part, this query might work if you are using MongoDB version 4.2 or greater,

db.hej.updateMany({}, [
  {
    $set: {
      "profile.firstName": {
        $concat: [
          { $toUpper: { $substr: ["$profile.firstName", 0, 1] } },
          { $substr: ["$profile.firstName", 1, { $strLenCP: "$profile.firstName" }] }
        ]
      },
      "profile.lastName": {
        $concat: [
          { $toUpper: { $substr: ["$profile.lastName", 0, 1] } },
          { $substr: ["$profile.lastName", 1, { $strLenCP: "$profile.lastName" }] }
        ]
      }
    }
  }
])

My suggestion is don’t copy-paste the query please try to understand the basic concept of the method from the documentation, it is not that hard to understand and in just a few minutes can understand the concept.

2 Likes

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.