-3
如何将集合中的所有数据写入外部文本文件?以下是集合:将BSON文档写入文件
var collection = _database.GetCollection<BsonDocument>("clicks");
如何将集合中的所有数据写入外部文本文件?以下是集合:将BSON文档写入文件
var collection = _database.GetCollection<BsonDocument>("clicks");
嗯,我想你想保存你的收藏在JSON格式。如果是这样的话,你可以这样做:
var r = collection.Find(new BsonDocument()).ToList();// Load all documents of that collection
System.IO.File.WriteAllText("file.txt",// path where you want to save the result
r.Aggregate<BsonDocument,string>("",(seed,document)=>seed+document.ToString()+"\n"));
如果您收藏有大量的文档,你可以考虑使用一个StringBuilder
由于性能方面的原因:
StringBuilder builder = new StringBuilder();
r.ForEach(d => builder.Append(d.ToString()+"\n"));
File.WriteAllText("file.txt",// path where you want to save the result
builder.ToString());
谢谢你所有我想要的:),你能否也建议我如何让文本文件中的结果更好? –
有什么样的研究你完成了吗?你已经尝试了什么? – Taegost
我试过这个: string data = collection.ToJson(); System.IO.File.WriteAllText(@“C:\ data.txt”,json); –
将这些信息添加到您的问题中,包括您尝试使用时发生了什么,以及您的期望如何 – Taegost