【问题标题】:How to query nested information in RavenDB?如何在 RavenDB 中查询嵌套信息?
【发布时间】:2011-10-21 10:17:35
【问题描述】:

我有以下名为 Reservation 的文件:

{
    "CustomerId": 1,
    "Items": [
        {
            "EmployeeId": "employees/1",
            "StartTime": "2011-08-15T07:20:00.0000000+03:00",
            "EndTime": "2011-08-15T07:40:00.0000000+03:00"
        },
        {
            "EmployeeId": "employees/1",
            "StartTime": "2011-08-15T07:40:00.0000000+03:00",
            "EndTime": "2011-08-15T09:10:00.0000000+03:00"
        },
        {
            "EmployeeId": "employees/3",
            "StartTime": "2011-08-16T07:20:00.0000000+03:00",
            "EndTime": "2011-08-16T11:35:00.0000000+03:00"
        }
    ]
    "ReservedAt": "2011-10-20T15:28:21.9941878+03:00"
}

另外我还有以下投影类:

public class ReservationItemProjection
{
    public string ReservationId { get; set; }
    public string CustomerId { get; set; }
    public string EmployeeId { get; set; }
    public DateTime StartTime { get; set; }
    public DateTime EndTime { get; set; }
}

如果我想找到匹配的,我应该写什么样的索引和查询 预订项目预测?例如:

// invalid example query:
var matches = docs.Query<ReservationItemProjection,
    ReservationItemProjectionsIndex>()
    .Where(x =>
        x.EmployeeId == "employees/1" &&
        x.StartTime >= minTime &&
        x.EndTime <= maxTime)
    .ToList();

请注意,我不希望获得预订文件列表,而是 ReservationItemProjection 对象的列表。 documentation 说:

但是,虽然获取与特定查询匹配的文档很有用,但我们可以做得更好。我想直接从索引中获取值,而不是获取文档本身,而不是获取完整的文档。

我已经尝试过使用这样的索引:

public class ReservationItemProjectionsIndex : 
    AbstractIndexCreationTask<Reservation, ReservationItemProjection>
{
    public ReservationItemProjectionsIndex()
    {
        Map = reservations => 
            from reservation in reservations
            from item in reservation.Items
            select new
            {
                ReservationId = reservation.Id,
                CustomerId = reservation.CustomerId,
                item.EmployeeId,
                item.StartTime,
                item.EndTime
            };
        Store(x => x.ReservationId, FieldStorage.Yes);
        Store(x => x.CustomerId, FieldStorage.Yes);
        Store(x => x.EmployeeId, FieldStorage.Yes);
        Store(x => x.StartTime, FieldStorage.Yes);
        Store(x => x.EndTime, FieldStorage.Yes);
    }
}

不知何故,我无法使查询和索引正常工作:它要么引发关于 无法从 ReservationItemProjection 转换为 Reservation 或者,当我 已经能够获得 ReservationItemProjection 对象,他们将拥有 包括所有保留中的所有项目,甚至有一个匹配项目,甚至 虽然我的查询有 Where-clause x.EmployeeId == "employees/1"。

总结:需要的索引是多少?索引是否只需要一个 Map 子句或 Reduce 或 TransformResults?如何用 C# 编写查询?

【问题讨论】:

    标签: indexing ravendb


    【解决方案1】:

    卡斯帕, 在 RavenDB 中,您正在查询 文档。虽然在技术上可以做你想做的事,但这样做通常没有意义,因为投射的信息没有所需的上下文来处理它。

    你想做什么?

    作为参考,索引类似于:

     from doc in docs.Items
     from reservation in doc.Reservations
     select new { reservation.EmployeeId, reservation.Start, reservation.End }
    

    然后,将 EmployeeId、Start 和 End 标记为 Store。

    现在,在您的查询中,发出:

      session.Query<...,...>().AsProjection<ReservationProjection>().ToList();
    

    AsProjection 调用会让数据库知道您需要索引中的值,而不是文档中的值

    【讨论】:

    • 这正是我想要的结果! AsProjection 是否被视为高级功能? Google 仅在 ravendb AsProjection 上返回两页结果。似乎该功能需要一些文档?
    • 谢谢,今天这个答案救了我的命!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-12
    相关资源
    最近更新 更多