0

I am trying to execute a user defined function on MongoDB from C# using RunCommand() method. But whenever I try to give the name of function or try it with eval it just throws an exception saying expected value but some character found.

Suppose below is my function on mongodb:

add(x1, x2){ return x1+x2;}

I tried executing as follows but failed :

_db.RunCommand($"add({x1},{x2})")

_db.RunCommand($"{ eval: "add({x1},{x2})"}")

_db.RunCommand($"{ $eval: "add({x1},{x2})"}")

  • Including the exact exception message can be useful in debugging for others. – Devon Bessemer Jul 12 '22 at 13:25
  • Hi here is the exception am seeing: 'JSON reader was expecting a value but found 'add' – Srivalli Gupta Jul 13 '22 at 03:20
  • I would get this running directly in mongodb(compass) first. Watch out for string interpolation syntax as it might get confusing. Maybe try these directly in mongo then play around with the c# side. https://www.mongodb.com/docs/manual/reference/operator/aggregation/add/ https://www.tutorialspoint.com/is-it-possible-to-sum-two-fields-in-mongodb-using-the-aggregation-framework – Netferret Jul 13 '22 at 15:59
  • the function works perfectly fine on mongo.. the example I stated is just for ref – Srivalli Gupta Jul 14 '22 at 09:15

0 Answers0