Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Raven DB Count Queries

Tags:

ravendb

I have a need to get a Count of Documents in a particular collection :

There is an existing index Raven/DocumentCollections that stores the Count and Name of the collection paired with the actual documents belonging to the collection. I'd like to pick up the count from this index if possible.

Here is the Map-Reduce of the Raven/DocumentCollections index :

from doc in docs
let Name = doc["@metadata"]["Raven-Entity-Name"]
where Name != null
select new { Name , Count = 1}

from result in results
group result by result.Name into g
select new { Name = g.Key, Count = g.Sum(x=>x.Count) }

On a side note, var Count = DocumentSession.Query<Post>().Count(); always returns 0 as the result for me, even though clearly there are 500 odd documents in my DB atleast 50 of them have in their metadata "Raven-Entity-Name" as "Posts". I have absolutely no idea why this Count query keeps returning 0 as the answer - Raven logs show this when Count is done

Request # 106: GET     -     0 ms - TestStore  - 200 - /indexes/dynamic/Posts?query=&start=0&pageSize=1&aggregation=None
like image 643
Zasz Avatar asked Apr 16 '11 18:04

Zasz


2 Answers

For anyone still looking for the answer (this question was posted in 2011), the appropriate way to do this now is:

var numPosts = session.Query<Post>().Count();
like image 170
Jay Querido Avatar answered Oct 23 '22 10:10

Jay Querido


To get the results from the index, you can use:

session.Query<Collection>("Raven/DocumentCollections")
       .Where(x=>x.Name == "Posts")
       .FirstOrDefault();

That will give you the result you want.

like image 33
Ayende Rahien Avatar answered Oct 23 '22 10:10

Ayende Rahien