默认情况下,您正在执行的查询将限制为相当数量的结果,这是RavenDB承诺在默认情况下为安全并且不会回传数百万条记录的一部分。
为了获取yoru数据库中特定类型的文档的数量,您需要一个特殊的map-reduce索引,它的工作是跟踪每个文档类型的计数。因为这种类型的索引直接处理文档元数据,所以在Raven Studio中定义这个更容易,而不是用代码创建它。
该索引的来源是this question,但我会在这里复制:
// Index Name: Raven/DocumentCollections
// Map Query
from doc in docs
let Name = doc["@metadata"]["Raven-Entity-Name"]
where Name != null
select new { Name , Count = 1}
// Reduce Query
from result in results
group result by result.Name into g
select new { Name = g.Key, Count = g.Sum(x=>x.Count) }
然后访问它在你的代码,你需要模仿双方创建匿名类型的结构类在Map和Reduce查询:
0:
public class Collection
{
public string Name { get; set; }
public int Count { get; set; }
}
然后,Ayende在回答中注意到,previously linked question,您可以从索引像这样得到的结果
但请记住,索引是异步更新的,因此在批量插入一堆文档后,索引可能过时。您可以通过在.Query(...)
之后加上.Customize(x => x.WaitForNonStaleResults())
来强制等待。
Raven Studio实际上从索引Raven/DocumentsByEntityName
获取此数据,该数据存在于每个数据库中,通过避开普通查询并获取索引上的元数据。你可以模仿这样的:
QueryResult result = docStore.DatabaseCommands.Query("Raven/DocumentsByEntityName",
new Raven.Abstractions.Data.IndexQuery
{
Query = "Tag:MyDocument",
PageSize = 0
},
includes: null,
metadataOnly: true);
var totalDocsOfType = result.TotalResults;
这QueryResult中包含了很多有用的数据:
{
Results: [ ],
Includes: [ ],
IsStale: false,
IndexTimestamp: "2013-11-08T15:51:25.6463491Z",
TotalResults: 3,
SkippedResults: 0,
IndexName: "Raven/DocumentsByEntityName",
IndexEtag: "01000000-0000-0040-0000-00000000000B",
ResultEtag: "BA222B85-627A-FABE-DC7C-3CBC968124DE",
Highlightings: { },
NonAuthoritativeInformation: false,
LastQueryTime: "2014-02-06T18:12:56.1990451Z",
DurationMilliseconds: 1
}
很多是你在任何查询相同的数据,如果你要求的统计,像这样的:
RavenQueryStatistics stats;
Session.Query<Course>()
.Statistics(out stats)
// Rest of query
但是,那么如何才能乌鸦工作室报告文件没有索引正确计数? – mll5
Raven Studio在内部使用Raven/DocumentsByEntityName索引上的统计信息,而不是基于标准LINQ的查询语义。我会更新我的答案。 –