向 Mongo 添加复杂的类
我在尝试向 Mongo 中的现有文档添加复杂类型时遇到了麻烦。
我有以下两节课。
public class UserObjectCollection {
[BsonId]
public Guid UserId { get; set; }
public Dictionary<string, object> UserObjects { get; set; }
public UserObjectCollection() {
UserObjects = new Dictionary<string, object>();
}
}
public class ComplexClass {
public string Bar { get; set; }
public int Foo { get; set; }
}
然后我创建一个新对象用于插入。
var bd = new UserObjectCollection() {
UserId = Guid.NewGuid(),
UserObjects = {
{ "data", 12 },
{ "data2", 123 },
{ "data3", new ComplexClass() { Bar= "bar", Foo=1234 } }
}
};
插入文档。
mongoCollection.Insert(bd.ToBsonDocument());
我得到了结果文件。
{“_id”:BinData(3,“t089M1E1j0OFVS3YVuEDwg==”),“UserObjects”:{“数据”:12, “数据2”:123,“数据3”:{“_t”:“ComplexClass”,“酒吧”:“酒吧”,“Foo”:1234} } }
文档插入正确。然后我修改其中一个值。
var query = Query.EQ("UserObjects.data", BsonValue.Create(12));
collection.FindAndModify(
query,
SortBy.Null,
Update.SetWrapped<ComplexClass>("data2", new ComplexClass() { Foo = -1234, Bar = "FooBar" }),
returnNew: false,
upsert: true);
数据库中显示的文档。
{“UserObjects”:{“数据”:12,“数据2”:{“酒吧”:“FooBar”,“Foo”:-1234}, "data3" : { "_t" : "ComplexClass", "Bar" : "bar", "Foo" : 1234 } }, "_id" : BinData(3,"W11Jy+hYqE2nVfrBdxn54g==") }
如果我尝试检索此记录时,我收到 FileFormatException。
var theDocument = collection.Find(query).First();
(未处理的异常:System.IO.FileFormatException:无法确定实际的t 要反序列化的对象的类型。 NominalType 是 System.Object,BsonType 是 Docum 条目。)。
与 data3 不同,data2 没有鉴别器。我在做什么?
I'm having trouble when trying to add complex types to existing documents in Mongo.
I have the following two classes.
public class UserObjectCollection {
[BsonId]
public Guid UserId { get; set; }
public Dictionary<string, object> UserObjects { get; set; }
public UserObjectCollection() {
UserObjects = new Dictionary<string, object>();
}
}
public class ComplexClass {
public string Bar { get; set; }
public int Foo { get; set; }
}
I then create a new object for insertion.
var bd = new UserObjectCollection() {
UserId = Guid.NewGuid(),
UserObjects = {
{ "data", 12 },
{ "data2", 123 },
{ "data3", new ComplexClass() { Bar= "bar", Foo=1234 } }
}
};
Insert the document.
mongoCollection.Insert(bd.ToBsonDocument());
And I get the resulting document.
{ "_id" : BinData(3,"t089M1E1j0OFVS3YVuEDwg=="), "UserObjects" : { "data" : 12,
"data2" : 123, "data3" : { "_t" : "ComplexClass", "Bar" : "bar", "Foo" : 1234 }
} }
The document inserts correctly. Then I modify one of the values.
var query = Query.EQ("UserObjects.data", BsonValue.Create(12));
collection.FindAndModify(
query,
SortBy.Null,
Update.SetWrapped<ComplexClass>("data2", new ComplexClass() { Foo = -1234, Bar = "FooBar" }),
returnNew: false,
upsert: true);
The document as it appears in the database.
{ "UserObjects" : { "data" : 12, "data2" : { "Bar" : "FooBar", "Foo" : -1234 },
"data3" : { "_t" : "ComplexClass", "Bar" : "bar", "Foo" : 1234 } }, "_id" : BinData(3,"W11Jy+hYqE2nVfrBdxn54g==") }
If I attempt to retrieve this record, I get a FileFormatException.
var theDocument = collection.Find(query).First();
(Unhandled Exception: System.IO.FileFormatException: Unable to determine actual t
ype of object to deserialize. NominalType is System.Object and BsonType is Docum
ent.).
Unlike data3, data2 doesn't have a discriminator. What am I doing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,如果驱动程序想要鉴别器,您可以通过将您的类强制转换为更新中的对象来提供它:
这将解决您的问题。
顺便说一句,您的更新实际上并未更新 UserObjects 中的 data2 字段,而是在文档中创建新的 data2 字段,以下代码应该可以正常工作:
Okay, if driver want discriminator you can give it by casting your class to object in update:
This will solve your issue.
BTW, your update not actually updating data2 field within
UserObjects
, it creating new data2 field within document, following code should work correctly:解串器无法根据 Bson 表示自行确定应使用什么类型。看看我前几天的提问吧。我认为它澄清了一些事情。实现 BsonSerialized 可以解决这个问题。
存储复合/嵌套对象图
The Deserializer can't figure out it self what type it should used based on Bson representation. Take a look at my question a few days ago. I think it clarifies a few things. Implementing BsonSerializable would solve the Issue.
Storing composite/nested object graph