how to get last character from a string in mongodb?
NickName:DennyHiu Ask DateTime:2018-04-06T11:47:41

how to get last character from a string in mongodb?

Data:

{code: "XXXXXXXX1", total: 400},
{code: "YYYYY2", total: 500}
{code: "ZZZZZZ3", total: 100}
{code: "AAA5", totala: 200}

I want to create an aggregate function to group the data above by its last character in the code field. code field is a string and can be varied in length. I only want to get its last index/number. Something like:

db.transactions.aggregate([
 {$project: {
   last_index: {$getMyLastCharInMyCode: "$code"},
   total: 1
 }},
 {$group: {_id: "$last_index", {total: {$sum: "$total"}}}}
])

I searched the internet and mongodb manuals, it seems impossible. Any ideas? Thank you

Copyright Notice:Content Author:「DennyHiu」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/49684970/how-to-get-last-character-from-a-string-in-mongodb

Answers
dnickless 2018-04-06T04:38:53

There you go:\n\ndb.transactions.aggregate({\n $addFields: {\n \"last_index\": { $substr: [ \"$code\", { $subtract: [ { $strLenCP: \"$code\" }, 1 ] }, 1 ] }\n }\n})\n",


More about “how to get last character from a string in mongodb?” related questions

how to get last character from a string in mongodb?

Data: {code: "XXXXXXXX1", total: 400}, {code: "YYYYY2", total: 500} {code: "ZZZZZZ3", total: 100} {code: "AAA5", totala: 200} I want to create an aggregate function to group the data ab

Show Detail

How do I get a unique 8 character string from a 24 character string?

I would like to get a 8 character string from a unique 24 character string so as to make a shortened URL. The 8 character string HAS to be based on the unique 24 character string. If I make a rando...

Show Detail

How to get the last character of a string?

How to get the last character of the string: "linto.yahoo.com." The last character of this string is "." How can I find this?

Show Detail

Get last character in string

I want to get the last character in a string MY WAY - 1) Get last index 2) Get character at last index, as a STRING. After that I will compare the string with another, but I won't include that part...

Show Detail

how do u get the last character of the last string in java?

I need to get the last character of the last input string. I wrote a piece of code that gets the last letter of the first string: public class LastCharacter { public static void main(String[] ...

Show Detail

get last character in a string

I am new to developing windows phone apps. Now I am creating a text messenger app with T9 keyboard, I already made design like the buttons. Now what I want is how can I get the last character in a ...

Show Detail

Splitting a string by newline character succeeds but removes the last character from the string

I am trying to split a string by newline character (\n) but it removes the last character from the string. How do I rectify this? I have tried replacing the newline character with a pair of the same

Show Detail

Get first and last character (number) from String android

I am trying to get the first and last character (number) from a String, but I don't know how to do this. public void getadrrss (){ SharedPreferences sharedPreferences = getPreferences(Context.

Show Detail

Get the second-to-last occurence of a character from a string

How do I get the second to last occurence of a character in a string? I want to use it to get aaa from www.example.com/example/abc/aaa

Show Detail

Unable to get last character of string

** Code: ** String test = "32132SSS654"; char Lastchar = test[test.Length - 1]; Assert.IsTrue((Lastchar).Equals("4")); I am trying to get the last character from the string then doing assert but...

Show Detail