Re: [mongodb-user] Using concat with aggregation...

5,132 views
Skip to first unread message

Shane Spencer

unread,
May 24, 2013, 8:58:16 PM5/24/13
to mongod...@googlegroups.com
> String(1) + String(1)
11



On Fri, May 24, 2013 at 11:55 AM, Steve Jerman <sje...@gmail.com> wrote:
I am trying to use an aggregation command to extract a monthly summary of data.

This projection works....

'$project'=>{ 
    'update'=> { 
    :year =>{ "$year" => "$fcsDate" },
    :month =>{ "$month" => "$fcsDate" }
    },
    'projectName'=>1,
'fcsDate'=>1,
   
    }},


  {"update":{"month":10,"year":2013},"fcsDate":"2013-10-14T00:00:00Z","_id":"519d06cf0c80e16a3800002f","projectName":"Project 3"}
....

What I really want is:
  {"update":"2013-10","fcsDate":"2013-10-14T00:00:00Z","_id":"519d06cf0c80e16a3800002f","projectName":"Project 3"}

But concat doesn't seem to work with numbers:

   {'$project'=>{ 
    'update'=> { 
    :year =>{ "$year" => "$fcsDate" },
    :month =>{ "$month" => "$fcsDate" }
    },
    'projectName'=>1,
'fcsDate'=>1,
   
    }},
   {'$project'=>{ 
    'month'=> { "$concat" => [ "$update.year","-","$update.month"]},
    'projectName'=>1,
'fcsDate'=>1,
   
    }},

/Library/Ruby/Gems/1.8/gems/mongo-1.8.6/lib/mongo/db.rb:548:in `command': Database command 'aggregate' failed: (errmsg: 'exception: $concat only supports strings, not NumberInt32'; code: '16702'; ok: '0.0'). (Mongo::OperationFailure)
from /Library/Ruby/Gems/1.8/gems/mongo-1.8.6/lib/mongo/collection.rb:667:in `aggregate'
from tree.rb:25

is there a function to convert a number to a string?
Steve

--
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com
See also the IRC channel -- freenode.net#mongodb
 
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Ross Lawley

unread,
Jun 27, 2013, 4:29:18 AM6/27/13
to mongod...@googlegroups.com
Hi,

$concat only works on strings and there is no expression in the aggregation framework to cast values into different types.

However, $substr forces coercion into a string, so you could use that:


db.test.drop()
db.test.save({"date": ISODate()})

db.test.aggregate(
  {$project: 
    {"year": {$substr: ["$date", 0, 4]}, 
     "month": {$substr: ["$date", 5, 2]}}}, 
  {$project: 
    {"year-date": {$concat: ["$year", "-", "$month"]}}}
)

Ross
Reply all
Reply to author
Forward
0 new messages