如何从 Raven DB 中的嵌套集合中查询项目? [英] How to query items from nested collections in Raven DB?

查看:26
本文介绍了如何从 Raven DB 中的嵌套集合中查询项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 2 个实体模型:

I have following 2 entity models:

      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 文档.

and of these Store is a document in my Raven Db. I need to create an index where I can query products by Name and the result should be partial Store documents containing only matching products.

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

So to be specific I need to ask Raven Db this: What stores have products containing this text, and what are those products in each store.

现在我可以创建一个索引,该索引为我提供包含匹配产品的 Store 文档,但它始终为我提供所有这些文档中的产品.

Now I can make an index which gives me Store documents with matching products but it always gives me ALL the products in those documents.

我想这是一个非常容易回答的问题,但我是 Raven Db 和文档数据库的新手,我无法完成这项工作.

I suppose this is a real easy one to answer but being new to Raven Db and document databases I just couldn't make this work.

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

There is an almost duplicate question here already but I still could not make the query/index work.

推荐答案

Mule,这是预期的,模型中的 Store Document 包含它的所有产品,如果您要求 Store Document,您将获得完整 Store Document.如果你只想得到你想要的东西的投影,你可以使用以下索引:

Mule, That is expected, a Store Document in your model contains all its products, and if you are asking for a Store Document, you'll get the full Store Document. If you want to just get a projection back for just the things that you want, you can use the following index:

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

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

Mark Name, Price, Created and Id as stored.

然后发出以下查询.

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

那只会给你匹配的产品.

That will give you only the matching products.

这篇关于如何从 Raven DB 中的嵌套集合中查询项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆