FindAll in MongoDB .NET Driver 2.0

前端 未结 3 680
既然无缘
既然无缘 2021-02-19 07:50

I want to query my MongoDB collection without any filter with MongoDB .NET Driver 2.0 but I didn\'t find a way. I have the following workaround but it looks weird :D

<         


        
3条回答
  •  悲哀的现实
    2021-02-19 08:45

    It works for me

    public class TestProductContext
    {
        MongoClient _client;
        IMongoDatabase _db;
    
        public TestProductContext()
        {
            _client = new MongoClient("mongodb://localhost:27017");
            _db = _client.GetDatabase("EmployeeDB");
    
        }
    
        public IMongoCollection Products => _db.GetCollection("Products");
    }
    
    public class DataAccess
    {
        private TestProductContext _testProductContext;
    
        public DataAccess(TestProductContext testProductContext)
        {
            _testProductContext = testProductContext;
        }
        public List GetProducts()
        {
            List pto = new List();
            var cursor = _testProductContext.Products.Find(new BsonDocument()).ToCursor();
            foreach (var document in cursor.ToEnumerable())
            {
                pto.Add(document);
            }
        }
    }
    

提交回复
热议问题