• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

184
Views
Is there a way to call a user-defined function in mongo from C# code?

I have added the following function in my Mongo database:

db.system.js.save({
    _id: "testFunction",
    value: function (int) {
           
    return int;
    }
})

I'm using the following packages in my C# project:

mongocsharpdriver 2.14.1 MongoDB.Driver 2.14.1

The function runs with no issue in a Mongo shell.

Is there a way to run it in a C# code? I have searched for a solution for a few hours but with no luck.

Please note that the Eval method/option is not available in above driver version, so please don't suggest it.

In addition, I'm aware of the CommandDocument class but couldn't figure out how to create it for calling the above function.

Thanks in advance !

almost 3 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I am also looking for the same but didn't find anything relevant so i decided to go with aggrgate() function in c# and suggesting to you also if you are not performing much complex queries in MongoDB function. The aggregate functions can also accept json format as MongoDB queries so you can try that something like this.

       database.GetCollection<BsonDocument>("collection").Aggregate()
          .Lookup("ForeignCollection", "LocalName", "ForeignName", "Object").Match("AnyFilter").Project(Builders<BsonDocument>.Projection.Exclude("field1").Include("field2")).ToList();
almost 3 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error