Find docs with no PartitionKey in Azure DocumentDb

by Frans Lytzen | 07/02/2017

When you are using Partitioned Collections in Azure DocumentDb you need to specify a Partition Key on each Document. At least, I thought you did. But, it turns out that you actually can save documents without a partitionkey. But if you do, you'll have a hard time retrieving or deleting them - until you meet Undefined.Value.
Note: This post is written for C#, I am not sure about the equivalent for other languages.


Details

If you create a Partitioned Collection in Azure DocumentDb you probably think that every document you save must have a partitionkey property and probably also that it must have a value. In this post I am dealing with the situation where you don't have a partition key property on your document at all, not the situation where you have one but you set it to null or an empty string.

For example, if you have created your collection with code similar to this;
var docCollection = new DocumentCollection()
{
Id = this.collectionName
};
docCollection.PartitionKey.Paths.Add("/partitionKey");
await docClient.CreateDocumentCollectionAsync(
UriFactory.CreateDatabaseUri(this.dbName),
docCollection);
and you then try to save an instance of a class that looks like this:
public class MyItem
{
[JsonProperty("id")]
public string Id { get; set; }

public string SomeValue { get; set; }
}
then you may expect to get an error. But, in fact, it will save just fine as I found out to my detriment after a major refactoring.

Now that you have that item in the database you will find it hard to retrieve it and even harder to delete it - until you meet your new friend Undefined.Value.

How to read the document:

MyItem item = (dynamic)client.ReadDocumentAsync(
UriFactory.CreateDocumentUri(DbName, CollectionName, id),
new RequestOptions() {
PartitionKey = new PartitionKey(Undefined.Value)
})
.Result.Resource;

How to delete the document:

client.DeleteDocumentAsync(
UriFactory.CreateDocumentUri(DbName, CollectionName, id),
new RequestOptions() { PartitionKey = new PartitionKey(Undefined.Value) });
Many thanks to Aravind Ramachandran for telling me about Undefined.Value.






Share this article

You Might Also Like

Explore more articles that dive into similar topics. Whether you’re looking for fresh insights or practical advice, we’ve handpicked these just for you.

How BDO UK Elevates Payroll Excellence with Payflow by NewOrbit

by George Elkington | 25/07/2025

Discover how BDO UK streamlined complex payroll operations with Payflow by NewOrbit — gaining full audit transparency and tailored workflows through a long-term product partnership.

Design, Code, AI: Behind the Scenes of Our Craft IT Logo Generator and Gallery

by Marcin Prystupa | 07/07/2025

A behind-the-scenes look at how we built two interactive apps for the Craft IT conference booth – and how I, a UX designer, ended up deep in React code with a little help from AI.

AI Isn’t Magic: Why Predictive Accuracy Can Be Misleading

by Frans Lytzen | 15/04/2025

One of the biggest misconceptions in AI today is how well it can actually predict things – especially things that are rare. This is most directly applicable to Machine Learning (as they are just statistical models) but the same principle applies to LLMs. The fundamental problem is the same and AI is not magic. In reality, AI’s predictive power is more complicated. One of the key challenges? False positives—incorrect detections that can significantly undermine the value of AI-driven decision-making. Let’s explore why this happens and how businesses can better understand AI’s limitations.

Cookie Settings

Contact Us

NewOrbit Ltd.
Hampden House
Chalgrove
OX44 7RW


020 3757 9100

NewOrbit Logo

Copyright © NewOrbit Ltd.