MongoDb BsonClassMap

发布于 2024-12-14 15:35:12 字数 1771 浏览 2 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花期渐远 2024-12-21 15:35:12

我怀疑您需要使用 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?

長街聽風 2024-12-21 15:35:12

事实证明,在开始使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文