▲相關特性MongoDB.Bson.Serialization.Attributes
MongoDB序列化特性官方文檔:
[BsonIgnore]
[BsonElement]
[BsonIgnoreExtraElements]
[BsonExtraElements]
[BsonRepresentation(BsonType.String)]
[BsonId]
2.可以忽略某些字段
[BsonIgnore]該標簽用來禁止字段序列化,不保存數據庫也不查詢,即忽略。
3.支持默認值以及取別名
[BsonElement] 字段加上該標簽,即使是private字段也會序列化(默認只序列化public字段),該標簽還可以帶一個string參數,給字段序列化指定別名。
[BsonIgnore]//忽略保存進數據庫,禁止字段序列化。 public Entity Entity [BsonIgnore]//組件類中 public NavGraph[] graphs; //默認private不會序列化,加上BsonElement就會序列化 [BsonElement("C")] [BsonIgnoreIfNull] private HashSet<Component> components = new HashSet<Component>();
元素順序
如果要精確控制BSON文檔中元素的順序,可以對BsonElement屬性使用Order named參數:
public class MyClass { [BsonElement("sp", Order = 1)] public string SomeProperty { get; set; } }
4.升級版本支持
[BsonIgnoreExtraElements] 該標簽用在class上面,反序列化時用來忽略多余的字段,一般版本兼容需要考慮,低版本的協議需要能夠反 序列化高版本的內容,否則新版本刪除字段,舊版本結構反序列化會出錯
5.支持額外的元素
[BsonExtraElements]
您可以將您的類設計為能夠處理反序列化期間在BSON文檔中可能發現的任何其他元素。為此,您必須具有BsonDocument(或IDictionary<string, object>)類型的屬性,並且必須將該屬性標識為應該包含找到的任何其他元素的屬性。按照慣例,可以命名該成員ExtraElements。例如:
public MyClass { // fields and properties [BsonExtraElements] public BsonDocument CatchAll { get; set; } }
6.表示,枚舉:
[BsonRepresentation(BsonType.String)]
對於某些.NET基本類型,您可以控制要用來表示值的BSON類型。例如,您可以指定將char值表示為BSON Int32還是一字符BSON字符串:
默認情況下,枚舉表示為其基礎值。換句話說,純枚舉將表示為整數值。但是,可以指示驅動程序將枚舉表示為字符串。
//告訴mongodb這個字段在數據庫中的類型是String [BsonRepresentation(BsonType.String)] public AppType AppType { get; set; } //告訴mongodb這個字段在數據庫中的類型是ObjectId [BsonRepresentation(BsonType.ObjectId)]
public class MyClass
{
[BsonRepresentation(BsonType.Int32)]
public char RepresentAsInt32 { get; set; }
[BsonRepresentation(BsonType.String)]
public char RepresentAsString { get; set; }
}
//默認是國際時間
[BsonDateTimeOptions(Kind = DateTimeKind.Local)]//單獨指定時區
[BsonDictionaryOptions(DictionaryRepresentation.ArrayOfArrays)]
DictionaryRepresentation有三個值可選:
[BsonRepresentation(BsonType.String)]
ET框架中,保存數據庫組件需要繼承 ISerializeToEntity接口。
[BsonDictionaryOptions(DictionaryRepresentation.ArrayOfArrays)] public Dictionary<int, int> MyAreaDict = new Dictionary<int, int>();
//class上面,反序列化時用來忽略多余的字段,否則新版本加了字段,舊版本結構反序列化會出錯 [BsonIgnoreExtraElements] public class CheckPointConfig : IConfig
7.指定默認值:[BsonDefaultValue(0L)]
您可以使用來為成員指定默認值BsonDefaultValueAttribute
:
8.忽略默認值:
[BsonIgnoreIfDefault]
[BsonId]
id編號產生器:[BsonId(IdGenerator = typeof(CombGuidGenerator))]
當您插入文檔時,驅動程序將檢查是否已為該Id
成員分配了值,如果沒有,則為該成員生成一個新的唯一值。由於Id
成員可以是任何類型,因此驅動程序需要借助IIdGenerator
來檢查成員是否Id
具有為其分配的值,並在必要時生成新值。該驅動程序具有以下內置的ID生成器:
ObjectIdGenerator
StringObjectIdGenerator
GuidGenerator
CombGuidGenerator
NullIdChecker
ZeroIdChecker<T>
BsonObjectIdGenerator
其中一些ID生成器會自動用於常用Id
類型:
要通過屬性指定ID生成器,請執行以下操作:
public class MyClass
{
[BsonId(IdGenerator = typeof(CombGuidGenerator))] public Guid Id { get; set; } }
或通過代碼:
BsonClassMap.RegisterClassMap<MyClass>(cm =>
{
cm.AutoMap();
cm.MapIdMember(c => c.Id).SetIdGenerator(CombGuidGenerator.Instance); });
[BsonElement] 字段加上該標簽,即使是private字段也會序列化(默認只序列化public字段,readonly),該標簽還可以帶一個string參數,給字段序列化指定別名。
[BsonIgnoreExtraElements] public abstract class ComponentWithId : Component { [BsonIgnoreIfDefault] [BsonDefaultValue(0L)]//指定默認值 [BsonElement] [BsonId]//定義為主鍵,字段映射,告訴mongodb這個字段在數據庫中對應_id,這個字段一般不需要在Json中體現出來,在序列化時被忽略 public long Id { get; set; } //默認是國際時間 [BsonDateTimeOptions(Kind = DateTimeKind.Local)]//單獨指定時區
MongoDB.Driver中的Bson特性
namespace MongoDB.Driver { [BsonElement("max")] public TValue Max => _max; [BsonElement("count")] public long Count { get; private set; }
Bson庫的指定序列化器:
有時需要使用特定的序列化程序,而不是讓BSON庫選擇。可以使用BsonSerializerAttribute
:
//這里是Bson庫的序列化器
namespace MongoDB.Driver.GeoJsonObjectModel {
[BsonSerializer(typeof(GeoJson2DGeographicCoordinatesSerializer))]
public class GeoJson2DGeographicCoordinates : GeoJsonCoordinates