using MongoDB.Bson; using MongoDB.Bson.IO; BsonDocument document = new BsonDocument(); BsonArray array = new BsonArray(); array.Add("value1"); array.Add("value2"); document.Add("arrayName", array); var writerSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.Strict }; var bsonWriter = new BsonBinaryWriter(new MemoryStream()); BsonSerializer.Serialize(bsonWriter, typeof(BsonDocument), document);
using MongoDB.Bson; using MongoDB.Bson.IO; var bsonWriter = new BsonBinaryWriter(new MemoryStream()); bsonWriter.WriteStartArray(); bsonWriter.WriteInt32(1); bsonWriter.WriteInt32(2); bsonWriter.WriteInt32(3); bsonWriter.WriteEndArray();In this example, we manually write a BSON array with the WriteStartArray method, followed by three integer values and the WriteEndArray method to complete the array.