Hi, @Michael_Evans,
Welcome to the MongoDB Community Forums. I understand that you’re trying to parse a JSON string into BSON related to an Atlas Search query. By default, the .NET/C# Driver does not allow duplicate element names in BsonDocument, but you can opt into it. Unfortunately this functionality is not exposed in BsonDocument.Parse or BsonDocument.TryParse. You can however copy our BsonDocument.Parse method and enable duplicate element name support:
using (var jsonReader = new JsonReader(json))
{
var context = BsonDeserializationContext.CreateRoot(jsonReader, cfg => cfg.AllowDuplicateElementNames = true);
var document = BsonDocumentSerializer.Instance.Deserialize(context);
if (!jsonReader.IsAtEndOfFile())
{
throw new FormatException("String contains extra non-whitespace characters beyond the end of the document.");
}
Console.WriteLine(document);
}
I have created CSHARP-5039 to implement these new overloads in our BSON library.
Sincerely,
James