Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Azure by (45.3k points)

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); 

1 Answer

0 votes
by (16.8k points)

Till the implementation of your "count" keyword, you should do your query in a store procedure on the server. 

Take care to not get all columns/properties in your query if you want only a count.

Select only the ID, as shown below:

  dc.CreateDocumentQuery(update.SelfLink, "SELECT c.id FROM c")

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...