Lets say I have an object which only contains an array
public class A
{
[BsonId]
public ObjectId Id;
public int[] arr;
public A()
{
arr = new [5];
}
}
when I want to update a certain index in the array by Id,
and the Id couldn't be found i want to insert the new object to the db, and when it is inserted
it suppose to have this provided Id and an arr with the provided value on the provided index.
so far so good, BUT, it doesn't happen.
//receiving ObjectId id, int input_index, int input_value
var filter = Builders<A>.Filter.Eq(x => x.Id, id);
var options = new UpdateOptions()
{
IsUpsert = true
};
var update = Builders<A>.Update.Set(x => x.arr[input_index], input_value);
//executing the query....
Instead, the Id is inserted OK, BUT the array is becoming an int value with the provided value, instead of an array.
Is that a known bug? is there anyway around it?
TIA
Refrence for older question but it was 2 years ago: here
EDIT
Im registering the arr with default vals:
if (!BsonClassMap.IsClassMapRegistered(typeof (A)))
{
BsonClassMap.RegisterClassMap<A>(map =>
{
map.AutoMap();
map.SetIgnoreExtraElements(true);
map.GetMemberMap(c => c.arr)
.SetDefaultValue(new int[5] {0, 0, 0, 0, 0});
});
}
Also tried to add tags instead of registerMap:
[BsonDefaultValue(new int[5] {0, 0, 0, 0, 0})]
[BsonIgnoreIfDefault]
public int[] arr {set;get;}
and Im receiving:
An error occurred while deserializing the arr property of class DatabaseManager.MongoEntities.A: Cannot deserialize a 'int[]' from BsonType 'Document'.