使用 BsonClassMaps 的 C# MongoDB 查询
我是 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过上述课程注册,您将在 mongodb 中拥有如下文档:
因此,如果您想通过
VoyageNumber
Id 获取Voyage
,您应该使用以下查询:注意: 如果映射所有字段,则类不需要
BsonClassMap.RegisterClassMap
因为它会默认序列化,无需任何注册。您还可以使用[BsonId]
、[BsonIgnore]
等属性进行自定义序列化。有关更多详细信息,请查看文档或提出其他问题;)。来自文档:
With above classes registration you will have document like this in mongodb:
So if you want get
Voyage
byVoyageNumber
Id you should use following query: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: