title: C# MongoDB - Read one record from database and write it to stream
body: <p>I'm trying to return all records from my database. This collection is comparably big (250MB), so I can't load it in the memory. My idea is to load every record individually and write it to the <code>NetworkStream</code>.</p>
  <p><strong>My problem:</strong></p>
  <p>The client closes connection after <code>await writer.WriteLineAsync();</code><br />
  (I tried to leave this line away but didn't work)</p>
  <p><strong>My Code:</strong></p>
  <pre class="lang-cs prettyprint-override"><code>var writer = new StreamWriter(stream, null, -1, true);
  await writer.WriteLineAsync($&quot;HTTP/1.1 200 OK&quot;);
  await writer.WriteLineAsync($&quot;Content-Type: application/json charset-utf8&quot;);
  await writer.WriteLineAsync($&quot;Transfer-Encoding: chunked&quot;);
  await writer.WriteLineAsync();
  
  using (var cursor = await collection.FindAsync(new BsonDocument()))
  {
      while (await cursor.MoveNextAsync())
      {
          var batch = cursor.Current;
          foreach (var document in batch)
          {
              byte[] data = Encoding.UTF8.GetBytes(document.ToString());
              await stream.WriteAsync(data, 0, data.Length);
          }
      }
  }
  </code></pre>
  <p>Thanks for helping!</p>