How to add dynamically calculated filed/column to the result-set documents? I don't need aggregation but if that's the only way with Mongo... yet I'm having troubles with $project aggregation stage. How to pass filed value(s) to a (non-aggregating) JS function in $project stage?
I'm looking for a way to transform the following to MongoDB query:
SELECT CountryName, MD5(CountryName) as md5 FROM Country
I've tried aggregation
db.getCollection('Country').aggregate([
{ "$project":{ a: "$Abbreviation", b: hex_md5("$Abbreviation")} }
])
Expected:
{
"_id" : 1.0,
"a" : "US",
"b" : "7516FD43ADAA5E0B8A65A672C39845D2"
},
{
"_id" : 2.0,
"a" : "JP",
"b" : "24D22E03AFB23EDB45C6C8CFA16A280E"
}
// but I get "$Abbreviation" treated as a string instead - not replaced by actual field value(s)
// MD5("$Abbreviation") == 668eddfec9d4b8a3bb099aca67b365d8
{
"_id" : 1.0,
"a" : "US",
"b" : "668eddfec9d4b8a3bb099aca67b365d8"
},
{
"_id" : 2.0,
"a" : "JP",
"b" : "668eddfec9d4b8a3bb099aca67b365d8"
}