You want to transform your field values to be keys in the results.
Unfortunately that's not possible, but there is a feature request for it that you can vote up in MongoDB Jira system.
Question
I am using Rails with MongoId, so that will be my syntax language.
My MongoDB objects are something like these:
{ "_id" : "Asset01_*00001", "total" : 124 ...}, { "_id" : "Asset01_*00002", "total" : 99 ...}, {...}
The project operator is:
{"project" => {"_id" => 1, "pair" => {"$add" => {"$_id" => "$total"}} }}
And group operator is:
{"$group" => { "invoice" => { "$push" => "$pair"} }}
But this throws the following error:
the $add operator does not accept an object as an operand
I know the meaning of the error, but have anyone an alternative for this problem?
The result that I would like is:
{ "_id" => 1, "pair" => {"Asset01_*00001" => 124, "Asset01_*00002" => 99 ... }
A hash of {asset_id => number}
Thanks
Solution
You want to transform your field values to be keys in the results.
Unfortunately that's not possible, but there is a feature request for it that you can vote up in MongoDB Jira system.