Working with MongoDB Driver on VB.NET


I’m kind of new working with the C# mongodb driver, basically i want to create a console application the monitors long running queries and emailing it to a certain group who monitors our server. just wanted to ask if how can i execute the command below using a mongodb driver… I’m already ok with the connection to the server , executing the command below im having trouble at… i want to execute the query below and save the result on a file.

db.currentOp({ active: true,op : { $ne : “none” },secs_running : { $gt : 60 },})?.inprog

Hi, @Daniel_Inciong,

Thank you for your question. You are using a shell function, which executes the $currentOp aggregation stage. In C#, you can do the same thing by executing $currentOp against the admin database. The .NET/C# Driver doesn’t have a helper function for $currentOp, but you can use AppendStage to add it to the pipeline and then add one or more Match stages to filter the results:

using System;
using MongoDB.Bson;
using MongoDB.Driver;

var client = new MongoClient();
var admin = client.GetDatabase("admin");

var query = admin.Aggregate()
                 .AppendStage<BsonDocument>("{$currentOp:{ }}")
                 .Match(x => x["active"] == true && x["op"] != "none" && x["secs_running"] > 60);
foreach (var result in query.ToList())

Hopefully this gets you started in the right direction.


1 Like