MongoDb BsonClassMap
我是 MongoDb 的新手,目前正在使用 CSharp 驱动程序(版本 1.2)。我的问题是在使用 BsonClass 映射时出现的。下面是我尝试执行的代码。我只是定义了一个想要映射到 BsonDocument 的自定义类型。
为了使用它,我利用了 BsonClassMap.RegisterClassMap()。当我点击 foreach 语句(尝试访问 FinAs() 结果中的第一个条目)时,出现以下错误:
“无法从 BsonType ObjectId 反序列化 Guid。”
据我了解,BsonClassMap 使用 GuidGenerator 来生成 Guid 类型的对象。为什么我会收到此错误?
请注意,执行插入时不会出现任何错误...执行插入后,newEmployee 会自动生成一个 EmployeeId。
这是我尝试运行的代码:
class Program{
static void Main(string[] args){
MongoServer server = MongoServer.Create();
MongoDatabase dataBase = server.GetDatabase("test");
MongoCollection<Employee>employees = dataBase.GetCollection<Employee>("employees");
BsonClassMap.RegisterClassMap<Employee>(cm =>
{
cm.MapProperty(c => c.Name);
cm.MapProperty(c => c.Email);
cm.MapIdProperty(c => c.EmployeeId);
});
var newEmployee = new Employee{ Name="Test", Email="[email protected]"};
employees.Insert(newEmployee);
foreach(Employee e in employees.FindAs<Employee>(Query.EQ("Name","Test")){
Console.Writeline(e.Name);
}
}
}
public class Employee
{
public Guid EmployeeId {get;set;}
public string Name {get;set;}
public string Email {get;set;}
}
I'm new to MongoDb and I'm currently using the CSharp drivers (version 1.2). My problems occur when using BsonClass map. Below is the code I'm tring to execute. I've simply defined a custom type I'd like to map to a BsonDocument.
In order to use this I'm taking advantage of BsonClassMap.RegisterClassMap(). When I hit the foreach statement (trying to access the first entry in the FinAs() results) I get the following error:
'Cannot deserialize Guid from BsonType ObjectId.'
From what I understand BsonClassMap uses GuidGenerator for objects of type Guid. Why am I getting this error?
Please note that the insertion is performed without any errors...and after performing the insert, newEmployee has an EmployeeId that's been automatically generated for it.
Here's the code I'm trying to run:
class Program{
static void Main(string[] args){
MongoServer server = MongoServer.Create();
MongoDatabase dataBase = server.GetDatabase("test");
MongoCollection<Employee>employees = dataBase.GetCollection<Employee>("employees");
BsonClassMap.RegisterClassMap<Employee>(cm =>
{
cm.MapProperty(c => c.Name);
cm.MapProperty(c => c.Email);
cm.MapIdProperty(c => c.EmployeeId);
});
var newEmployee = new Employee{ Name="Test", Email="[email protected]"};
employees.Insert(newEmployee);
foreach(Employee e in employees.FindAs<Employee>(Query.EQ("Name","Test")){
Console.Writeline(e.Name);
}
}
}
public class Employee
{
public Guid EmployeeId {get;set;}
public string Name {get;set;}
public string Email {get;set;}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我怀疑您需要使用 SetIdMember 来识别你的 ID 字段。您不使用
ObjectId
值而不是 Guid 的任何特殊原因?I suspect you need to use SetIdMember to identify your id field. Any particular reason you aren't using
ObjectId
values instead of Guids?事实证明,在开始使用 BsonClassMap 之前,我的集合中存储了一些其他文档。他们的 ObjectId 以 MongoDB 原生的格式存储……不幸的是,这种格式无法解析为 Guid。为了修复该错误,我最终清除了所有旧条目。
Turns out I had some additional documents stored in my collection before I started using BsonClassMap. Their ObjectId was stored in a format native to MongoDB....unfortunately this format couldn't be parsed to a Guid. To fix the error I ended up wiping out all old entries.