I have an odd issue with mongodb .net driver. When using source code listed below, all properties in TestObject are stored as UUID, except
"guid" property in dictionary.
So, default behavior with BsonSerializer.RegisterSerializer(new GuidSerializer(GuidRepresentation.Standard)); yields following result in mongo. As you can see only one guid is stored as binary. Rest are UUIDs.

But, when I use
BsonSerializer.RegisterSerializer(new GuidSerializer(GuidRepresentation.Standard));
BsonDefaults.GuidRepresentation = GuidRepresentation.Standard;
It yields following result in mongo. As you can see, all guids are stored as UUID. So, why behaviors are different in the first case?
While I do not have an issue using BsonDefaults.GuidRepresentation = GuidRepresentation.Standard;
, it's marked as an Obsolete, so I would need to know what is the solution for this issue, since I want all guids to be stored as UUIDs.
Any idea how can I fix this?
Full source:
var connectionString = "mongodb://localhost:27017";
BsonSerializer.RegisterSerializer(new GuidSerializer(GuidRepresentation.Standard));
//BsonDefaults.GuidRepresentation = GuidRepresentation.Standard; //uncomment to work properly
// Create a MongoClient object.
var client = new MongoClient(connectionString);
// Get your database (replace "yourDatabase" with your database name).
var database = client.GetDatabase("test");
// Get your collection (replace "yourCollection" with your collection name).
var collection = database.GetCollection<TestObject>("guids");
TestObject testObject = new TestObject
{
Id = 1,
Name = "aaaaaaaaaa",
Guids = new List<Guid>()
{
Guid.NewGuid(),
Guid.NewGuid()
},
Objects = new Dictionary<string, object>()
};
testObject.Objects.Add("guids", new List<Guid>()
{
Guid.NewGuid(),
Guid.NewGuid()
});
testObject.Objects.Add("guid", Guid.NewGuid());
// Insert the TestObject instance into the collection.
collection.InsertOne(testObject);
Console.WriteLine("TestObject inserted successfully.");
Console.ReadKey();
public class TestObject
{
[BsonId]
public int Id { get; set; }
public string Name { get; set; }
public IEnumerable<Guid> Guids { get; set; }
public Dictionary<string, object> Objects { get; set; }
}
