user306080 user306080 - 1 year ago 160
C# Question

MongoDb + C# - get functions of the database

Good day all,

The questions is: How could I access functions' definitions from the MongoDb database object?

const string connectionString = "mongodb://localhost";

// Create a MongoClient object by using the connection string
var client = new MongoClient(connectionString);

//Use the MongoClient to access the server
MongoServer server = client.GetServer();

// Use the server to access the 'local' database
var database = server.GetDatabase("local");

//What next?

Answer Source

If you mean get them as in get the value of the function code itself, they are stored in the system.js collection for a database. Get them all as with any other collection:


In C#:

var database = server.GetDatabase("databaseName");

var collection = database.GetCollection<BsonDocument>("system.js");
var filter = new BsonDocument();
var count = 0;
using (var cursor = await collection.FindAsync(filter))
    while (await cursor.MoveNextAsync())
        var batch = cursor.Current;
        foreach (var document in batch)
            // process document

If you mean call them within code, if system.js contains an object { _id: "two", value: "function(x, y) { return x + y; }" }, you can call the function by doing as follows in the loop:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download