How to use MongoDB C # driver without specifying a class

I am using MongoDB C # 2.0 driver. I am trying to get a collection without specifying a type or class. Note:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Core;
using MongoDB.Driver.Linq;
using MongoDB.Shared;

namespace Meshplex.Service.DataWarehouse
    public class ProfileControllerMongoDB
        private IMongoDatabase _mongoDb;
        private IMongoCollection _myCollection;
        //private IMongoCollection<ClassHere> _myCollection;

        public ProfileDataControllerMongoDB()
            _mongoDb = GetMongoDatabase();
            _myCollection = _mongoDb.GetCollection(GetCollectionName());
            //_myCollection = _mongoDb.GetCollection<ClassHere>("Collection");

        public async Task<string> GetAllRecords()
            //This should return json
            return await _myCollection.Find(new BsonDocument());


As you can see, I have to specify the class when declaring IMongoCollection

. Is there a way to use the MongoDB driver without specifying a class?


source to share

1 answer

MongoDb supports the type dynamic

in the generic parameter.

IMongoCollection<dynamic> _myCollection = _mongoDb.GetCollection<ClassHere>("Collection");




All Articles