Massoud Mazar

Sharing The Knowledge


Azure DocumentDB (Cosmos DB): Cross partition query does not return data if TOP is not specified

When querying Azure DocumentDB (recently renamed to Cosmos DB, to make you really believe it can handle anything you throw at it), best practice is to have a Partition Id. In case of high volume scenarios, it's mandatory to partition your data.

You may select a good partition key, but there are always scenarios where you need to query your data without knowing the Partition Id. These cross partition queries, although slower, are possible by specifying EnableCrossPartitionQuery = true in your FeedOptions, when you are creating your query:

protected IQueryable<TData> CreateQuery(int pageSize, string continuationToken = null)
	var uri = UriFactory.CreateDocumentCollectionUri("database", "children");
	IQueryable<Child> query = _client.CreateDocumentQuery<Child>(uri, 
		new FeedOptions
			EnableCrossPartitionQuery = true,
			MaxItemCount = pageSize,
			RequestContinuation = continuationToken
	query = query.Where(p => p.FirstName == "john" && p.ParentId = "Smith Family");
	return query;

Options I'm using in above code are to allow my doing a paged query. Then I can create a query and get my results:

var query = CreateQuery(pageSize);
var exec = query.AsDocumentQuery();
var skipped = 0;

while (exec.HasMoreResults && results.Count < pageSize)
	var response = await exec.ExecuteNextAsync<Child>();
	if (!response.Any())

	if (skip - skipped >= response.Count)
		skipped += response.Count;
		results.AddRange(response.Skip(skip - skipped).Take(pageSize - results.Count));
		skipped += skip - skipped;

In above code, I was trying to simulate Linq's Skip+Take functionality. I used the PageSize to simulate "Take" and by skipping pages, achieve "Skip", but that is not why I decided to write this post. Problem started when my query did not include filter for "Parent Id". Something like this:

query = query.Where(p => p.FirstName == "john");

And my query was not getting any results. Funny thing is, if I copy the SQL from my above query and run it in DocumentDB Query Explorer in Azure portal, it return the data.

After a few hours of investigation, I noticed when I used to have the Linq's "Take" extension in my query, I was getting data, so I added it back with a large value (in this case 1000) and started to get the data again:

query = query.Where(p => p.FirstName == "john").Take(1000);

 Unfortunately I don't have any explanation for this behavior, but I thought this may help other confused developer like myself.

Develop Go App Engine API using Visual Studio Code

If you are using Go extension for Visual Studio Code to develop Google App Engine backend code on a Windows machine, you may have encountered strange problems related to environment variables GOROOT and GOPATH, specially when you want to use 3rd party libraries and expect the extension to correctly highlight code errors.

After lots of experiments, I ended up with the following setup which seems to be working as desired: More...

Using D3 charting components with ReactJS

ReactJS seems to have picked up some die hard fans and recently I was looking at how to use D3 for charting in a React based UI. There are a few implementation of some of the D3 libraries and I picked reactd3 for my experiment. The documentation site has some examples of how this can be done, but they all use ReactDOM.render to directly render to a DOM element you have put in your HTML template. My preferred approach is to not rely on existence of a predefined HTML tag in the template, but use an element which is rendered by my chart component. Here is what I ended up doing:More...

Sql Server Spatial and Unmanaged Memory Leak

Encountering memory leaks in a .net application is not very common, but when dealing with one which is not common and you cannot find any mention of it online, it is even harder to resolve. I had one of these encounters this week and took me a couple of days to find a solution for it.

A product I'm working on, makes extensive use of SQL server DbGeometry types to store and manipulate GIS data. DbGeometry type is provided in the System.Data.Entity.Spatial library, which can be added to your .net code using the Microsoft.SqlServer.Types NuGet package. After a trivial change in the code I realized it is failing on the QA server where it was deployed to. Strangely there was no failure on the development machine. Failure on the server was surfacing as processing taking so ling it would timeout. Observing the server health revealed unprecedented memory use when this new part of code was being executed. More...

Restrict Access to your Google App Engine API using Service Account

I needed to access my restricted Google App Engine API from an Android App using a Service account. Let's say, I don't want to ask my users to authenticate with their Google accounts to my Android App, and I also don't want any third party access my App Engine API. All the documentation I found online was about how to authenticate with backend using users, but nothing about using service accounts. Even on many StackOverFlow articles people say it is not possible.

So, here is summary of steps: More...

From T-SQL to NoSQL

I know I have been stuck with SQL server for too long and a lot behind in adopting newer database technologies, but late is better than never. While updating a web application which uses SQL Server, I realized the current relational structure of my database is not really the optimal solution. I have more than 120 million rows of data in one of the tables which represent Option Chains for Stocks, one row per Option. I store this data as snapshots in time and do not change them after they are stored. Anyone familiar with this type of data knows that these individual rows are not really interesting by themselves and they are normally looked at alongside others which belong to the same Stock, and with same expiration. In real world you are presented with the whole chain (see an example of such data here). More...

Quick and easy Custom 404 page for MVC 5

While MVC makes it easy and clean to create a professional looking website, it seems to be a little harder to incorporate custome error pages (e.g. 404 page) which are part of general look and feel of your site. There are many good write-ups on how to do this (for example, but I was looking for an easy way to point to one of my views as the 404 page.

What I ended up doing was to create a controller for my error views and use the httpErrors section of web.config to point to it: More...

Adding and removing Self Signed Certificates in IIS

It took me a little bit to get a handle of creating and using Self Signed Certificates for IIS on a Windows Server 2012. Problem was not so much about running a couple of commands to create the certificate, but more related to where things are and what to do when things go wrong.

This page provides 2 commands to create a self signed certificate to be used by IIS. I had to make the key length equal to 2048 so Chrome browser does not complain about the keys:  More...

Simulate authentication for Roku player

Few weeks ago I decided to add a channel to my Roku box to play live videos from a Free-To-Air Satellite TV channel. This specific channel has a live video stream from their website which requires authentication. My challenge was to build a Roku app using provided SDK to simulate the authentication process of the website so I can grab the URL of the live stream. (URL has a token attached to it which requires authentication).

To accomplish this More...

Trick Google Chrome to save password

Today I was trying to get Google chrome to save my username and password for a web site but it would not offer me to save it. This web site uses the browser popup for login credentials (which is caused by the server sending back HTTP 401: Unauthorized). Chrome would show the popup and I could login after entering credentials, but would not offer to save it. My settings related to saving passwords was correct and this site was not in the "Do not save" list. I tried many solutions, like deleting files from App Data folder or even uninstalling and re-installing chrome. Until I found an interesting behaviour by accident. More...