irom
irom

Reputation: 3606

mongodb string to number

I have document in mongodb 2.6.11 including array of string i.e.

{ cpu: [ '0', '2', '4', '0', '0', '2', '0', '4', '0' ],
 con: [ '232', '2396', '17082', '339', '5', '1738', '503', '4', '0' ] }

How do I convert them to numbers without saving in actual collection so I can use them in $project (aggregation) and later use in $group to calculate $avg?

db.checkpointstest3.aggregate([
    {$unwind: "$cpu"}])

I have correct $group , but it works on numbers not strings

Upvotes: 1

Views: 1471

Answers (1)

Alex
Alex

Reputation: 21766

Currently the aggregation pipeline does not allow for type conversion, see this JIRA ticket. If changing the type of your field is not an option, you either have to fall back on a map reduce query or write your own aggregation using forEach.

Upvotes: 2

Related Questions