Consider the following C# class:
public class Role
{
public string Id { get; set; }
public IEnumerable<string> Users { get; set; }
}
How do I configure that Users maps to an array of ObjectIds in MongoDB using BsonClassMap.RegisterClassMap<Role>? I am able to configure that the scalar property Id maps to an ObjectId, but can’t figure out how to do the same for a sequence (array):
BsonClassMap.RegisterClassMap<Role>(m =>
{
m.MapIdProperty(r => r.Id).SetRepresentation(BsonType.ObjectId);
// How do I map r.Users to an array of ObjectId??
m.MapProperty(r => r.Users);
});
The answer is to call
SetSerializationOptionswith a correctly initializedArraySerializationOptionsobject: