alexandre-spieser / mongodb-generic-repository

An example of generic repository implementation using the MongoDB C# Sharp 2.0 driver (async)
MIT License
315 stars 87 forks source link

MongoDB stored procedure #25

Closed Ahmad-Aktaa closed 4 years ago

Ahmad-Aktaa commented 4 years ago

Hi Alexandre,

How to create MongoDB stored procedure or calling a Stored Procedure in MongoDB via "mongodb-generic-repository" ?

alexandre-spieser commented 4 years ago

Not sure if you can add through the driver, I have not used this feature before. However you can try to call a function using the following code:

public class TestRepository : BaseMongoRepository, ITestRepository
{
        public void MyCustomFunction()
        {
            var command = new JsonCommand<BsonDocument>($"{{ eval: \"addNumbers({1},{2})\" }}");
            var result = MongoDbContext.Database.RunCommand(command)["retval"].ToInt32();
        }
        ......
}

(untested, just looked at https://stackoverflow.com/questions/23709351/calling-a-stored-procedure-in-mongodb-via-c-sharp)

Basically, you can access all the driver methods in your implementation through MongoDbContext.Database.

Good luck