/// <summary> /// Remove document _id if contains a "empty" value (checks for autoId bson type) /// </summary> private bool RemoveDocId(BsonDocument doc) { if (doc.TryGetValue("_id", out var id)) { // check if exists _autoId and current id is "empty" if ((_autoId == BsonAutoId.Int32 && (id.IsInt32 && id.AsInt32 == 0)) || (_autoId == BsonAutoId.ObjectId && (id.IsNull || (id.IsObjectId && id.AsObjectId == ObjectId.Empty))) || (_autoId == BsonAutoId.Guid && id.IsGuid && id.AsGuid == Guid.Empty) || (_autoId == BsonAutoId.Int64 && id.IsInt64 && id.AsInt64 == 0)) { // in this cases, remove _id and set new value after doc.Remove("_id"); return(true); } } return(false); }