MongoDB query to capitalise first letter in existing database

后端 未结 1 1480
长发绾君心
长发绾君心 2021-01-29 10:19

Here is the problem,

I want to capitalise the first letter of a name in my existing database, just wanted to know if there is any query so that i can ma

1条回答
  •  无人及你
    2021-01-29 10:35

    it may not be the best solution. the only hiccup in below suggestion is to get "3" of $substr:["$name1",1,3] dynamically. but gives you a start?

    db.toupper.aggregate([{$project:{name:{$concat:[{$toUpper:{$substr:["$name1",0,1]}},{$substr:["$name1",1,**3**]}]}}}])
    

    below is the result

     db.toupper.find()
    
     "_id" : ObjectId("5767ca0badb381a5cc0d19cd"), "name1" : "lean" }
     "_id" : ObjectId("5767ca3aadb381a5cc0d19ce"), "name1" : "lean" }
    
     db.toupper.aggregate([{$project:{name:{$concat:[{$toUpper:{$substr:["$name1",0,1]}},{$substr:["$name1",1,3]}]}}}])
    
     "_id" : ObjectId("5767ca0badb381a5cc0d19cd"), "name" : "Lean" }
     "_id" : ObjectId("5767ca3aadb381a5cc0d19ce"), "name" : "Lean" }
    

    0 讨论(0)
提交回复
热议问题