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