我如何将一个项目推送到数组中? 我发现我只能插入基本值(String、Int32、Int64、Boolean),但我不能将自定义类的实例插入到数组中。
//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
);
最佳答案
PushWrapped
随驱动程序 1.0(似乎)一起提供,只需将您的类转换为 BsonDocument
:
Update.PushWrapped<i_Player>("_player", myPlayer);
如果您使用 Update.Push
,则需要手动执行:
Update.Push("_player", myPlayer.ToBsonDocument());
我正在使用 ToBsonDocument()
将一些类对象转换为 BsonValue
。
所以,选择你更喜欢的吧..
关于c# - MongoDB c# Update.PushWrapped,怎么用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7730096/