我是MongoDB的新手。我有一个具有以下定义的对象
[BsonDiscriminator("user")]
public Class BrdUser
{
[BsonId(IdGenerator = typeof(StringObjectIdGenerator))]
public string ID { get; set; }
[BsonElement("username")]
public string UserNm { get; set; }
[BsonElement("email")]
public string EmailAdrs { get; set; }
.
.
.
public IList<Question> Questions{ get; set; } //<-- Un sure as to what Bson type should this be
}
其中Questions
是另一个定义为:
[BsonDiscriminator("userques")]
public class Question
{
[BsonId(IdGenerator = typeof(StringObjectIdGenerator))]
public string ID { get; set; }
[BsonElement("title")]
public string Title{ get; set; }
[BsonElement("desc")]
public string Desciption{ get; set; }
}
我的问题是在映射时,我应该使用什么属性,以便User对象使用Question对象反序列化。 C#Driver中没有[BsonDocument]
属性。
答案 0 :(得分:7)
我不确定你在哪里但是试试:
var brdDoc = new BrdUser ();
brdDoc.UserNm = "invisible";
brdDoc.EmailAdrs = "someone@womewhere.com";
brdDoc.Questions = new []{ new Question{ Title = "Q", Desciption = "Question to ask" } };
var bsonDocument = brdDoc.ToBsonDocument ();
var jsonDocument = bsonDocument.ToJson ();
Console.WriteLine (jsonDocument);
它将打印:
{
"_id" : null,
"username" : "invisible",
"email" : "someone@womewhere.com",
"Questions" : [{ "_id" : null, "title" : "Q", "desc" : "Question to ask" }]
}