2011-10-11 15 views
2

我如何推送一個數組中的項目? 我看到我只能插入基本的值(String,Int32,Int64,Boolean),但我無法在數組中插入自定義類的INSTANCE。MongoDB c#Update.PushWrapped,如何使用它?

//in this way, it work: 
var myPlayer = new i_Player(); 
this.mongo_collection.FindAndModify(
Query.EQ("_id",ID), 
SortBy.Ascending("_id"), 
Update.PushWrapped<i_Player>("_player", myPlayer), 
true 
); 

// in this way, don't work because i_Player is not an BsonValue but is my CLASS! 
var myPlayer = new i_Player(); 
this.mongo_collection.FindAndModify(
Query.EQ("_id",ID), 
SortBy.Ascending("_id"), 
Update.Push("_player", myPlayer), 
true 
); 

回答

2

PushWrapped與司機1.0到來(好像),只是你的類轉換爲BsonDocument:如果您在使用Update.Push

Update.PushWrapped<i_Player>("_player", myPlayer); 

如果你需要做手工:

Update.Push("_player", myPlayer.ToBsonDocument()); 

我正在使用ToBsonDocument()將某些類對象轉換爲BsonValue

所以,只是選擇你更喜歡什麼..