var bsonDocument = new BsonDocument { { "name", "John" }, { "age", 30 }, { "address", new BsonDocument { { "street", "123 Main St" }, { "city", "New York" }, { "state", "NY" } } } }; var stream = new MemoryStream(); var writer = new BsonWriter(stream); BsonSerializer.Serialize(writer, bsonDocument); stream.Position = 0; var reader = new BsonReader(stream); reader.ReadStartDocument(); // reads the start of the bson document
var bsonDocument = new BsonDocument { { "name", "Mary" }, { "age", 35 }, { "address", new BsonDocument { { "street", "456 North Ave" }, { "city", "Chicago" }, { "state", "IL" } } } }; var bytes = bsonDocument.ToBson(); var reader = new BsonReader(new MemoryStream(bytes)); reader.ReadStartDocument(); // reads the start of the bson documentIn this example, we create a new BSON document similar to the first example, but instead of serializing it using a BsonWriter, we use the ToBson method of the BsonDocument class to get its byte representation. We then create a BsonReader by passing the byte stream, and call the ReadStartDocument method to read the start of the BSON document.