$function(aggregation)

db.players.insertMany([
   { _id: 1, name: "Miss Cheevous",  scores: [ 10, 5, 10 ] },
   { _id: 2, name: "Miss Ann Thrope", scores: [ 10, 10, 10 ] },
   { _id: 3, name: "Mrs. Eppie Delta ", scores: [ 9, 8, 8 ] }
]); db.players.find( {$expr: { $function: {
      body: function(name) { return hex_md5(name) == "15b0a220baa16331e8d80e15367677ad"; },
      args: [ "$name" ],
      lang: "js"
} } } ); 
Error: error:  {
"ok" : 0,
"err msg" :  "Unrecognised expression '$function'"",
"code" : 168,
"codeName" : "InvalidPipeline Operator" 
}

Above is the error while making query on collection , ‘players’.

Hi Arindam,

You can only use the $function aggregator within an $aggregation expression. So instead of db.players.find try: db.players.aggregate().

I wrote a blog post that goes into it a lot more that you can find here: https://developer.mongodb.com/how-to/use-function-accumulator-operators/ :slight_smile:

1 Like