[英]Return entire collection from MongoDB
using the official mongo / c# drivers - what is the best way of returning an entire collection, and what is the best way of storing the data? 使用官方的mongo / c#驱动程序-返回整个集合的最佳方法是什么,以及存储数据的最佳方法是什么? I've seen some examples of iterating over a collection and returning a particular value, like this:
我看到了一些遍历集合并返回特定值的示例,如下所示:
var collection = db.getCollection("users").findAll();
foreach (var value in collection){
value = collection["key"];
...
}
but what if I don't know the key names - and I just want to return the collection? 但是,如果我不知道键名,而我只想返回集合,该怎么办?
You dont need to know the key names when returning a collection. 返回集合时,您不需要知道键名。
public static void ReadCollectionDataUsingBson(string collectionName, string databaseName)
{
MongoDatabase database = CreateDatabase(databaseName);
MongoCollection<BsonDocument> collection = database.GetCollection<BsonDocument>(collectionName);
foreach (BsonDocument document in collection.FindAll())
{
foreach (string name in document.Names)
{
BsonElement element = document.GetElement(name);
Console.WriteLine("{0}: {1}", name, element.Value);
}
Console.WriteLine();
}
}
Note: CreateDatabase() function is user defined, so i have just shown you the required code over here. 注意:CreateDatabase()函数是用户定义的,因此我在这里向您展示了所需的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.