【问题标题】:How to query items from nested collections in Raven DB?如何从 Raven DB 的嵌套集合中查询项目?
【发布时间】:2011-08-03 15:23:05
【问题描述】:

我有以下 2 个实体模型:

      public class Store : IModel
      {
        public string Id { get; set; }
        public string Name { get; set; }
        public string MainPageUrl { get; set; }
        public ICollection<Product> Products { get; set; }

      }

      public class Product : IModel {
          public string Id { get; set; }
          public string Name { get; set; }
          public double Price { get; set; }
        public DateTime Created { get; set; }
}

这些 Store 是我的 Raven Db 中的一个文档。 我需要创建一个索引,我可以在其中按名称查询产品,结果应该是仅包含匹配产品的部分 Store 文档。

所以具体来说,我需要问 Raven Db:哪些商店有包含此文本的产品,每个商店的产品是什么。

现在我可以创建一个索引,为我提供包含匹配产品的 Store 文档,但它总是为我提供 ALL 这些文档中的产品。

我想这是一个很容易回答的问题,但是对于 Raven Db 和文档数据库来说,我只是无法完成这项工作。

这里已经有一个几乎重复的question,但我仍然无法使查询/索引工作。

【问题讨论】:

    标签: ravendb


    【解决方案1】:

    骡子, 这是预期的,模型中的 Store Document 包含其所有产品,如果您要求 Store Document,您将获得 full Store Document。 如果您只想获得您想要的东西的投影,您可以使用以下索引:

    from store in docs.Stores
    from product in store.Products
    select new { product.Name, product.Price, product.Created, store.Id }
    

    将名称、价格、创建和 ID 标记为已存储。

    然后发出以下查询。

    session.Query<StoreProduct>()
      .Where(s=>s.Name == name)
      .AsProjection<StoreProduct>()
      .ToList();
    

    这只会为您提供匹配的产品。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多