查询子文档Ravendb

时间:2014-07-25 10:41:20

标签: c# linq ravendb

int start = 0;
var current = _db.Query<Record>().Take(1024).Skip(start).ToList();

这很好用,返回一个包含子文档/ pocos列表的列表,如下图所示:

keyword collection shown

这显示了关键字集合,即关键字pocos列表。但是,我想返回所有关键字,所以我尝试这样做:

int start = 0;
var current = _db.Query<Keyword>().Take(1024).Skip(start).ToList();

然而什么都没有归还?所有导入都可以,所有内容都在编译和运行,只是没有列出...

编辑 实现静态索引并使用map / reduce代码的后续部分

  Map = record => from keyword in record.Keywords 
  // here visual studio doesn't allow subdocuments, 
  // only offers up System.Object methods after record ?

截图: enter image description here

2 个答案:

答案 0 :(得分:1)

您只能查询RavenDB中的 root 文档。 现在,您可以查询包含子对象中特定值的所有根文档(子文档,没有这样的东西),但您仍然要求提供根文档。 / p>

例如,查询看起来像:给我所有包含关键字“nice”的记录:

session.Query<Record>().Where(x=>x.Keywords.Contains("Nice")).ToList();

你可以在所有记录中询问他们所拥有的关键词:

session.Query<Record>().Select(x=> x.Keywords).ToList();

但你总是从根文档开始。

答案 1 :(得分:1)

如果您想直接查询关键字,那么您可以编写索引。

public class KeywordsIndex : AbstractIndexCreationTask<Record, KeywordsIndex.Result>
{
    public KeywordsIndex()
    {
        Map = records => from record in records
            from keyword in record.Keywords
            select new 
                {
                    Keyword = keyword
                }
    }

    public class Result
    {
        public Keyword Keyword { get; set; }
    }
}

设置:

documentStore.Initialize();
documentStore.ExecuteIndex(new KeywordsIndex());

然后调用它:

using (var session = documentStore.OpenSession())
{
    session.Query<KeywordsIndex.Result, KeywordsIndex>().Select(r => r.Keyword).ToList();
}

这样您就可以实际查询关键字并排序或过滤等。 有关静态索引的详细信息,请参阅:http://ravendb.net/docs/2.0/client-api/querying/static-indexes/defining-static-index

您可以直接从索引返回关键字,但不确定,但如果可以,它可能对您有好处。

相关问题