Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get record count in Azure DocumentDb

It seems like 'select count(*) from c' in the SQL queries allowed by documentdb in the azure site and through the documentdb explorer (https://studiodocumentdb.codeplex.com/) is not supported. To date, the only way to get a record count that I have found is from code (see below). However, there are enough files in our collection now that this is crashing. Is there a way to get a count on how many documents in a collection that works more than my solution?

DocumentClient dc = GetDocumentDbClient(); var databaseCount = dc.CreateDatabaseQuery().ToList(); Database azureDb = dc.CreateDatabaseQuery().Where(d => d.Id == Constants.WEATHER_UPDATES_DB_NAME).ToArray().FirstOrDefault();  var collectionCount = dc.CreateDocumentCollectionQuery(azureDb.SelfLink).ToList();  DocumentCollection update = dc.CreateDocumentCollectionQuery(azureDb.SelfLink).Where(c => c.Id == "WeatherUpdates").ToArray().FirstOrDefault();  var documentCount = dc.CreateDocumentQuery(update.SelfLink, "SELECT * FROM c").ToList();  MessageBox.Show("Databases: " + databaseCount.Count().ToString() + Environment.NewLine                 +"Collections: " + collectionCount.Count().ToString() + Environment.NewLine                 + "Documents: " + documentCount.Count().ToString() + Environment.NewLine,                   "Totals", MessageBoxButtons.OKCancel);  
like image 633
ProgramTheWorld Avatar asked Jan 02 '15 16:01

ProgramTheWorld


2 Answers

This is now possible in the year 2017 of our lord.

SELECT VALUE COUNT(1) FROM c

[ 1234 ]

like image 87
micah Avatar answered Sep 23 '22 11:09

micah


Actually works at this point:

SELECT COUNT(c.id) FROM c 
like image 28
Mike Katchourine Avatar answered Sep 22 '22 11:09

Mike Katchourine