In my import function, i am replacing existing documents or adding new ones using the Upsert option:
var builder = Builders<FacilityDocument>.Filter;
var filter = builder.Eq(x => x.Language.LCID, lcid)
& builder.Eq(x => x.Name, facility.Name)
& builder.Eq(x => x.NameDetailed, facility.NameDetailed)
& builder.Gte(x => x.ImportedDate, new DateTime(DateTime.Now.Year, 1, 1));
collection.ReplaceOne(filter, facility, new UpdateOptions { IsUpsert = upsert });
Now in my POCO class, i have it configured to use GUID instead of an ObjectID, like this:
[BsonId]
[BsonIgnoreIfDefault]
public Guid ID { get; set; }
The problem is that in the database, instead of generating a GUID, its defaulting back to ObjectId:
And what it should be doing, is this (which works when using the regular Insert method):
Much appreciated if anyone have a solution for this.
Update
This is my entire facilitydocument class:
[BsonId]
[BsonIgnoreIfDefault]
public Guid ID { get; set; }
public string Name { get; set; }
public string NameDetailed { get; set; }
public CommuneDocument Commune { get; set; }
public string Address { get; set; }
public string Email { get; set; }
public string Homepage { get; set; }
public FacilityTypeDocument FacilityType { get; set; }
public FacilityPlacement FacilityPlacement { get; set; }
public int CoursesCount { get; set; }
public CoursesData[] CoursesData { get; set; }
public OwnershipDocument Ownership { get; set; }
public OperationDocument Operation { get; set; }
public string ExternalRemarks { get; set; }
public GisLocationData GisLocationData { get; set; }
public string ContactPersonName { get; set; }
public string ContactPersonEmail { get; set; }
public string InternalRemarks { get; set; }
public bool Active { get; set; }
public LanguageDocument Language { get; set; }
public DateTime ImportedDate { get; set; }
public string ImportedBy { get; set; }
public string UpdatedBy { get; set; }
public DateTime UpdatedDate { get; set; }
public List<ChangeLog> ChangeLog { get; set; }
Update 2
If i set my ID before the replace method, i get an error like this:
A write operation resulted in an error. The _id field cannot be changed from {_id: BinData(3, BF515DEF5743F547BD3EABB1A89DAC4D)} to {_id: BinData(3, 6364DF16640A4346B62E6B866BF76069)}
This is how i set it:
facilityDocument.ID = Guid.NewGuid();