I have a file which is a collection dump from MongoDB. I am serializing it like this:

var serializedBsonArray = MongoDB.Bson.Serialization.BsonSerializer.Deserialize<BsonArray>(File.ReadAllText($"./the file"));

I am modifying a few values which is not an issue. When I attempt to write the BsonArray I get an exception: System.InvalidOperationException : An Array value cannot be written to the root level of a BSON document.

Here is the call:

using var fs = File.OpenWrite("outarray.bson"); using var bw = new BsonBinaryWriter(fs); MongoDB.Bson.Serialization.BsonSerializer.Serialize(bw, serializedBsonArray); <--Throws here

I have tried other serializers but I really need to keep this as a BsonArray so that the output can be imported into Mongo via Compass.