MongoDB c# Update.PushWrapped,如何使用?
我如何将一个项目推送到数组中? 我发现我只能插入基本值(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
);
How i can push an item in an Array?
I see that i can insert only basic value (String, Int32, Int64, Boolean) but i can't insert into an array an INSTANCE of custom class.
//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
);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
PushWrapped
与驱动程序 1.0 一起提供(似乎),只需将您的类转换为BsonDocument
:如果您使用
Update.Push
,您需要这样做手动:我正在使用
ToBsonDocument()
将某些类对象转换为BsonValue
。所以,就选择你更喜欢什么吧..
PushWrapped
coming with driver 1.0 (seems) and simply convert your class toBsonDocument
:In case if you using
Update.Push
you need to do it manually:I am using
ToBsonDocument()
to convert some class object toBsonValue
.So, just choose what do you like more..