简体   繁体   English

如何在C#中获取IMongoCollection中的所有项目?

[英]How to get all items from IMongoCollection in C#?

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

Mongodb.driver 2.0中没有FindAll函数。

您应该找到像users.Find(new BsonDocument()).ToListAsync();

丑陋直白的方法:

await (await users.FindAsync(_ => true)).ToListAsync()

You can use LINQ 您可以使用LINQ

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

Or maybe if you are in somewhere near Mongo Driver 2.7 version the below query would compile. 或者,如果您在Mongo Driver 2.7版本附近,可能会编译以下查询。 (note also that generic parameter of Users is redundant here) (另请注意,这里的用户通用参数是多余的)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM