Not sure how I should say this, but I want to make my code cleaner and simpler.
Right now I have to make a new Connection for every method:
public static bool CheckUser(ulong userid)
{
var client = new MongoClient("");
var db = client.GetDatabase("Aureliadb");
var collection = db.GetCollection<User>("user");
var check = collection.Find(x => x.id == userid).ToList();;
if (check.Count == 0)
return false;
else
return true;
But I would like to have something like a static connection so I can access the database wherever I want. I tried something like this:
public static dynamic AureliaDB(string selectedCollection)
{
var client = new MongoClient("");
var db = client.GetDatabase("Aureliadb");
var collection = db.GetCollection<User>(selectedCollection);
return collection;
}
Here I get a collection back that I selected. That didn't work with the collection.Find() so, my question is: how do I avoid writing the same code over and over again?
PS: I am quite new to C# and coding...
Here is my solution:
static IMongoClient client = new MongoClient("");
static IMongoDatabase db = client.GetDatabase("Aureliadb");
static IMongoCollection<User> userCollection = db.GetCollection<User>("user");