mongodb exception: $concat only supports strings not NumberInt32 solution

  • 2020-05-09 19:32:30
  • OfStack

This problem occurred today when operating aggregation with mongodb. I wanted to format the date, for example, "2013-10-1704:41:37 UTC" becomes "October 17th".


'fdate' => { '$concat' => ['$date.month', ' month ', '$date.day', ' day '] }

exception: $concat only supports strings, not NumberInt32

The original $concat can only manipulate strings and does not support numeric types. The solution is to use $substr


$date like '2013-10-13 11:17:18 UTC' 'fdate' => { '$concat' => [ {$substr=>['$date', 5, 2]}, ' month ', {$substr=>['$date', 8, 2]}, ' day '] }

$substr accepts two arguments, one for the starting point of the string and one for the length of the string it intercepts.


Related articles: