簡體   English   中英

C# MongoDb 從 stream 插入整個集合

[英]C# MongoDb insert entire collection from a stream

I have a process that archives MongoDb collections by getting an IAsyncCursor and writing the raw bytes out to an Azure Blob stream. 這似乎非常有效並且有效。 這是工作代碼。

var cursor = await clientDb.GetCollection<RawBsonDocument>(collectionPath).Find(new BsonDocument()).ToCursorAsync();
while (cursor.MoveNext())
    foreach (var document in cursor.Current)
    {
        var bytes = new byte[document.Slice.Length];
        document.Slice.GetBytes(0, bytes, 0, document.Slice.Length);
        blobStream.Write(bytes, 0, bytes.Length);
    }

However, in order to move this data from the archive back into MongoDb, the only way I've figured out how to do it is to load the entire raw byte array into a memory stream and then .InsertOneAsync() in to MongoDb. 這對於較小的 collections 確實有效,但對於非常大的 collections 我收到 MongoDb 錯誤。 此外,這顯然不是很有效的 memory。 有什么方法可以將 stream 原始字節數據轉換為 MongoDb,或者像我在讀取時那樣使用 cursor?

var rawRef = clientDb.GetCollection<RawBsonDocument>(collectionPath);
using (var ms = new MemoryStream())
{
    await stream.CopyToAsync(ms);
    var bytes = ms.ToArray();
    var rawBson = new RawBsonDocument(bytes);
    await rawRef.InsertOneAsync(rawBson);
}

如果集合太大,這是我得到的錯誤。

MongoDB.Driver.MongoConnectionException : An exception occurred while sending a message to the server.
---- System.IO.IOException : Unable to write data to the transport connection: An established connection was aborted by the software in your host machine..
-------- System.Net.Sockets.SocketException : An established connection was aborted by the software in your host machine.

無需將 stream 整體復制到 byte-Array 並將其解析為RawBsonDocument ,您可以逐個解析文檔,例如:

while (stream.Position < stream.Length)
{
    var rawBson = BsonSerializer.Deserialize<RawBsonDocument>(stream);
    await rawRef.InsertOneAsync(rawBson);
}

stream 將以一個為單位讀取。 上面的示例將文檔直接插入到數據庫中。 如果要批量插入,可以在列表中收集合理數量的文檔並使用InsertManyAsync

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM