Search code examples
c#mongodbtypesmongodb-.net-driver

MongoDB C# create a function that I can pass a type/class name to it as paramater


I have a function (see below).

It works great for what I need it to do, and I have another one that is almost the exact same thing except for these 2 lines:

        var collection = MongoDB.GetCollection<RocketRequest>("RocketRequest");

        var filter = Builders<RocketRequest>.Filter.Eq("RocketRequestId", 4);

I want to be able to somehow make the function dynamic so I can pass it the so I can use this same function for multiple purposes/types and not need to create a new function for every single one.

For the above 2 lines I want to see if it is possible to somehow pass the object in the collection to the function as a parameter?

Or worst case have a switch statement to check a string value I passed and say if this, set it to that, else set it to something else. Problem with doing this is I can not create the variable inside the switch statement, if I try to create it outside the switch I have to do it as an empty object (then set the collection to what I need inside the switch) and I can not get that to work either.

       public static int MongoReadData(string[] MongoListOfFieldsToDisplay)
    {
        int CountRecords = 0;

        // create connection TO MongoDB
        MongoClient MongoClientConn = new MongoDatabaseConnection().mongoConn();
        var MongoDB = MongoClientConn.GetDatabase("Viper");
        var collection = MongoDB.GetCollection<RocketRequest>("RocketRequest");

        var filter = Builders<RocketRequest>.Filter.Eq("RocketRequestId", 4);
        //var filter = Builders<RocketRequest>.Filter.Empty;
        var result = collection.Find(filter).ToListAsync().Result;

        // do here so it is after the filtering
        CountRecords = Convert.ToInt32(collection.Find(filter).Count());

        return CountRecords;

    }// end MongoReadData    

Solution

  • I was able to find code that allowed me to pass the class as NEW to make it work to do what I needed. See the new version of the function and the call:

    So I call the same function and can use it to query 2 separate collections.

    MongoReadData_ResponseTEST<RocketResponse>(Mongo_ListOfFieldsToDisplay_RocketResponse, "RocketResponse", "RocketResponseId");
    MongoReadData_ResponseTEST<RocketRequest>(Mongo_ListOfFieldsToDisplay_RocketRequest, "RocketRequest", "RocketRequestId");
    
    
    public static int MongoReadData_ResponseTEST<T>(string[] MongoListOfFieldsToDisplay, string CollectionName, string PKToSearchOn) where T : class, new()
    {
        int CountRecords = 0;
    
        // create connection TO MongoDB
        MongoClient MongoClientConn = new MongoDatabaseConnection().mongoConn();
        var MongoDB = MongoClientConn.GetDatabase("Viper");
        var collection = MongoDB.GetCollection<T>(CollectionName);
    
        var filter = Builders<T>.Filter.Eq(PKToSearchOn, 4);
        //var filter = Builders<RocketRequest>.Filter.Empty;
        var result = collection.Find(filter).ToListAsync().Result;
    
        // do here so it is after the filtering
        CountRecords = Convert.ToInt32(collection.Find(filter).Count());
    
        return CountRecords;
    
    }// end MongoReadData