c# - MongoDB c# Update.PushWrapped,怎么用?

标签 c# mongodb mongodb-.net-driver

我如何将一个项目推送到数组中? 我发现我只能插入基本值(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/

相关文章:

node.js - 在mongodb中获取数组而不是[object OBJECT]类型

node.js - Connect-mongo session 不会被自动删除

c# - 如何在MongoDB的FindOne中使用SetField For C# Driver

c# - MVC中的mongodb连接池

c# - 带有SoundPlayer的C#音频-真的就是所有这些吗?

c# - 存储几十个列的配置值的最佳方法是什么?

c# - 池回收上的 ASP.NET Websocket 行为

mongodb - Mongodb 将字符串转换为数字以供查询

mongodb-.net-driver - 为 NEventStore 设置 mongodb 持久性时需要为每种事件类型注册 BsonClassMap?

c# - 如何以编程方式检索组策略设置?