使用 BsonClassMaps 的 C# MongoDB 查询

发布于 2024-10-31 13:33:02 字数 1311 浏览 5 评论 0原文

我是 MongoDB 新手,想知道如何使用注册的 BsonClassMap 查询域对象。 考虑以下映射:

BsonClassMap.RegisterClassMap<VoyageNumber>(cm =>
            { cm.MapField<string>(p => p.Id); });

BsonClassMap.RegisterClassMap<Schedule>(cm =>
        { cm.MapField<DateTime>(p => p.EndDate); cm.MapField<DateTime>(p => p.StartDate); });

BsonClassMap.RegisterClassMap<Voyage>(cm =>
            { cm.MapIdField<VoyageNumber>(p => p.VoyageNumber); cm.MapField<Schedule>(p => p.Schedule); });

域实体是“Voyage”。在这个简单的示例中,它由复杂类型“Schedule”(带有开始日期和结束日期)和 VoyageNumber(带有字符串 id 字段)组成。 “VoyageNumber”是实体的标识符。

现在我可以插入一个新航程了:

MongoCollection<Voyage> mongoVoyages = context.MyDB.GetCollection<Voyage>("Voyages");

mongoVoyages.Insert<Voyage>(voyage);

我可以通过以下方式检索一个航程:

MongoCollection mongoVoyages = context.MyDB.GetCollection("Voyages");

BsonDocument result = mongoVoyages.FindOneAs<BsonDocument>();
BsonDocument sched = result["Schedule"].AsBsonDocument;
DateTime start = sched["StartDate"].AsDateTime;
//etc...

现在当然我想通过 ID 搜索航程。如何使用上面显示的映射来实现这一点?我尝试了类似的方法但失败了:

Query.EQ("VoyageNumber", someStringID)

我使用的是官方 C# 驱动程序 1.0。

I'm new to MongoDB and was wondering about querying for domain objects with registered BsonClassMaps.
Consider the following Mappings:

BsonClassMap.RegisterClassMap<VoyageNumber>(cm =>
            { cm.MapField<string>(p => p.Id); });

BsonClassMap.RegisterClassMap<Schedule>(cm =>
        { cm.MapField<DateTime>(p => p.EndDate); cm.MapField<DateTime>(p => p.StartDate); });

BsonClassMap.RegisterClassMap<Voyage>(cm =>
            { cm.MapIdField<VoyageNumber>(p => p.VoyageNumber); cm.MapField<Schedule>(p => p.Schedule); });

The domain entity is "Voyage". In this simple example it consists of a complex type "Schedule" (with start date and end date) and a VoyageNumber (with a string id field).
"VoyageNumber" is the identifier for the entity.

Now I can insert a new voyage just fine with:

MongoCollection<Voyage> mongoVoyages = context.MyDB.GetCollection<Voyage>("Voyages");

mongoVoyages.Insert<Voyage>(voyage);

I can retrieve one Voyage via:

MongoCollection mongoVoyages = context.MyDB.GetCollection("Voyages");

BsonDocument result = mongoVoyages.FindOneAs<BsonDocument>();
BsonDocument sched = result["Schedule"].AsBsonDocument;
DateTime start = sched["StartDate"].AsDateTime;
//etc...

Now of course I'd like to search a voyage by Id. How can I achieve this with the mappings shown above? I tried something like this and failed:

Query.EQ("VoyageNumber", someStringID)

I'm using the official C# driver 1.0.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

我最亲爱的 2024-11-07 13:33:02

通过上述课程注册,您将在 mongodb 中拥有如下文档:

{
  "_id": {
    "_id": someStringID
  },
  "Schedule": {
    "EndDate": "Sun, 10 Apr 2011 13:06:25 GMT +03:00",
    "StartDate": "Sun, 10 Apr 2011 13:06:25 GMT +03:00"
  }
}

因此,如果您想通过 VoyageNumber Id 获取 Voyage,您应该使用以下查询:

Query.EQ("_id._id", someStringID)

注意: 如果映射所有字段,则类不需要 BsonClassMap.RegisterClassMap 因为它会默认序列化,无需任何注册。您还可以使用 [BsonId][BsonIgnore] 等属性进行自定义序列化。有关更多详细信息,请查看文档或提出其他问题;)。

来自文档:

您可以创建此类映射
你自己或者只是允许类映射
第一次时自动创建
需要(称为“自动映射”)。你可以
施加一定的控制
自动映射过程可以通过
装饰你的班级
序列化相关属性或通过
使用初始化代码(属性
使用起来非常方便但是对于
那些喜欢保持序列化的人
其域类的详细信息是
确信能做任何事
带属性也可以做到
没有他们)

With above classes registration you will have document like this in mongodb:

{
  "_id": {
    "_id": someStringID
  },
  "Schedule": {
    "EndDate": "Sun, 10 Apr 2011 13:06:25 GMT +03:00",
    "StartDate": "Sun, 10 Apr 2011 13:06:25 GMT +03:00"
  }
}

So if you want get Voyage by VoyageNumber Id you should use following query:

Query.EQ("_id._id", someStringID)

Note: You no need BsonClassMap.RegisterClassMap for class if you map all fileds because it will be serialized by default without any registration. Also you can do custom serialization using attributes like [BsonId], [BsonIgnore]. For more details check documentation or ask another question ;).

From documentation:

You can either create this class map
yourself or simply allow the class map
to be created automatically when first
needed (called "automapping"). You can
exert some control over the
automapping process either by
decorating your classes with
serialization related attributes or by
using initialization code (attributes
are very convenient to use but for
those who prefer to keep serialization
details out of their domain classes be
assured that anything that can be done
with attributes can also be done
without them)

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