![](/img/trans.png)
[英]How to pull ObjectId from an Array in a BsonDocument with C# MongoDriver 2.5
[英]How to create a BsonDocument from stream (c#)
我想將從服務器讀取的電子郵件插入到MongoDB。 我使用MimeKit從服務器獲取電子郵件。 嘗試直接將其插入mimeMessage.ToBsonDocument()
我得到System.InvalidOperationException: 'Timeouts are not supported on this stream.'
所以我猜想消息對象使用內部流從服務器讀取數據。
我現在想使用內存流首先將數據寫入流,然后從該流創建BsonDocument。
我在這里閱讀到最好的方法是使用BsonDocument.ReadFrom(ms);
但是編譯器在BsonDocument中找不到方法“ ReadFrom”。
在較新的版本中是否替換了此方法? 什么? 或從流創建BsonDocument的最佳方法是什么?
謝謝
根據http://api.mongodb.com/csharp/2.0/html/Methods_T_MongoDB_Bson_BsonDocument.htm上的文檔,似乎您需要將流轉換為字符串,然后使用Parse()
方法:
string bsonText;
using (var reader = new StreamReader (ms))
bsonText = reader.ReadToEnd ();
var bsonDocument = BsonDocument.Parse (bsonText);
就是說,我認為您不能只將MimeMessage
寫入流,然后期望將其解析為BsonDocument
。 當您將MimeMessage
寫入流時,它將采用MIME格式,而不是BSON格式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.