Create and Call Stored Procedures in cosmos mongodb through .net Core

I am trying to create a Stored Procedure and call it through .net core application.
This is how I created the SP -
var sysJs = azureDatabase.GetCollection(“System.js”);
var code = File.ReadAllText("…path\sum.js");
var codeDocument = new BsonDocument(“value”, new BsonJavaScript(code));
codeDocument.Add(new BsonElement("_id", “mathSum”));
sysJs.InsertOne(codeDocument);

2 Failed ways of calling it -

  1. BsonValue bv = azureDatabase.Eval(“addNumbers(2,3)”);
    where azureDatabase is Database reference object

  2. var command = new JsonCommand($"{{ eval: “addNumbers({1},{2})” }}");
    var result = azureDatabase.RunCommand(command)[“retval”].ToInt32();

which gives an exception that eval is not supported

Hi, welcome to MongoDB Community Forums, @Divya_Noor

Azure Cosmos DB is a Microsoft cloud database product with partial emulation for popular database APIs like MongoDB, Cassandra, and Gremlin. Cosmos’ MongoDB API provides an incomplete emulation of MongoDB using an entirely independent server implementation. MongoDB drivers and tools are not tested against CosmosDB, so if you encounter a compatibility error you should report this to Cosmos support.

Based on the error message you received, it’s not clear to me exactly what you are trying to do, and whether MongoDB or CosmosDB supports it. Are trying to run the (deprecated since 3.0) db.eval() command?

If full MongoDB feature support is important to your use case, please consider using MongoDB Atlas on Azure for a managed data service.

Asya

2 Likes

Great points by Asya.

Also take a look at this post by Maxime talking about incompatibilities between MongoDB and Cosmos DB at the API level - Not able to restore indexes using mongorestore - #2 by MaBeuLux88

The current Cosmos DB API for MongoDB is lagging a whole version behind latest MongoDB 5.0

MongoDB Atlas is an excellent service that helps you run cloud-agnostic workloads in addition to offering latest MongoDB version since it’s all built by the same team.

Thanks,
Mahi

2 Likes