Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So i need to connect to mongo db with my c# code where i pass the guid as an argument and fetch the job data which matches that guid from the mongo db document.

PS : I am both new to Mongo DB and C#. So basically need to know how get information from mongo db document post connection to the database through guid value.

What I have tried:

C#
using MongoDB.Driver;
using MongoDB.Bson;

var dbClient = new MongoClient("mongodb://username:password@localhost:27017/admin?authSource=admin&readPreference=primary&appname=MongoDB%20Compass&directConnection=true&ssl=false");
var dbList = dbClient.ListDatabases().ToList();
IMongoDatabase db = dbClient.GetDatabase("MyDB");
var DocName = db.GetCollection<BsonDocument>("DocName");

var filter = Builders<BsonDocument>.Filter.Eq("guid", "00bf02eb-e71b-4a30-a36b-ad87134b05dd");

var command = new BsonDocument { { "dbstats", 1 } };
var result = db.RunCommand<BsonDocument>(command);

Console.WriteLine("The List of Databases are:");

foreach (var item in dbList)
{
    Console.WriteLine(item);
}

Console.WriteLine(result.ToJson());
Posted
Updated 29-Nov-22 0:21am
v3
Comments
Richard MacCutchan 28-Nov-22 9:33am    
And? What happens when you run this code? Please do not expect people to be able to guess what you see on your screen.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900