RavenDB. How to load document with only 5 items from inner collection?

本秂侑毒 提交于 2019-12-06 03:44:15

问题


Here is a document in the store:

{
    "Name": "Hibernating Rhinos", 
    "Employees": [
        { "Name": "Ayende" },
        { "Name": "John" },
        { "Name": "Bob" },
        { "Name": "Tom" },
        { "Name": "Lane" },
        { "Name": "Bill" },
        { "Name": "Tad" }
     ]
}

It is easy to load this document with or without Employees collection, but how to load only part of inner collection? For instance, first 5 items:

{
    "Name": "Hibernating Rhinos", 
    "Employees": [
        { "Name": "Ayende" },
        { "Name": "John" },
        { "Name": "Bob" },
        { "Name": "Tom" },
        { "Name": "Lane" }
     ]
}

回答1:


Not directly, not.

What you can do is define the following index:

from company in docs.Companies
from emp in company.Employees
select new { Compnany = company.Name, Employee = emp }

You can then query the index for the first five employees




回答2:


You can use Live Projections feature of RavenDB. Put this query in TransformResults function of your index: I assume that your document name is Company.

TransformResults = (database, companies) => from c in companies
                                        select new {Company=c,Employees=c.Employees.Take(5)};


来源:https://stackoverflow.com/questions/3379357/ravendb-how-to-load-document-with-only-5-items-from-inner-collection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!