MongoDB c#更新.PushWrapped,如何使用它
本文关键字:何使用 PushWrapped 更新 MongoDB | 更新日期: 2023-09-27 18:11:02
如何在数组中推送一个项目?我看到我只能插入基本值(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
。
所以,选择你更喜欢的…