简体   繁体   English

MongoDB的序列化设置

[英]Serialization settings for MongoDB

Is there any way to serialize only private fields of my object which has DataMember attributes in MongoDB? 有什么方法可以仅序列化在MongoDB中具有DataMember属性的对象的私有字段?

string json = 
    item.ToJson(
        new MongoDB.Bson.IO.JsonWriterSettings() 
        { 
            GuidRepresentation = GuidRepresentation.Standard, 
            Indent = false, 
            OutputMode = MongoDB.Bson.IO.JsonOutputMode.JavaScript 
        }
     );

To prevent a public field from being serialized use the BsonIgnore attribute: 为了防止公共字段被序列化,请使用BsonIgnore属性:

public class Car
{
    public string Brand;

    public string Model;

    [BsonIgnore]
    public double Price;
}

In the sample code above the price field will be ignored when the class is serialized. 在上面的示例代码中,当类被序列化时,价格字段将被忽略。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM