How to get all items from IMongoCollection in C #?

var users = database.GetCollection<ApplicationUser>("users"); 

There is no FindAll function in Mongodb.driver 2.0.

+11
c # mongodb
source share
3 answers

You should find an empty filter, for example users.Find(new BsonDocument()).ToListAsync();

+9
source share

Ugly simple approach:

 await (await users.FindAsync(_ => true)).ToListAsync() 
+5
source share

You can use LINQ

 var collection = _db.GetCollection("users"); return (from x in collection.AsQueryable() select x["something"]).toList(); 

Or maybe if you are somewhere close to the version of Mongo Driver 2.7, the following request will be compiled. (note also that the generic Users parameter is redundant here)

 List<string> q2=(from x in collection.AsQueryable<users>() select x.Name).ToList(); 
0
source share

All Articles