Retrieve documents without Metadata properties in Azure CosmosDB

One of the interesting queries that i got from my colleague is that how to get rid of the metadata properties when retrieving documents from Cosmosdb. It seemed like a very reasonable expectation to have the option with the document “GET” API call to be able to retrieve exactly what he created using the document “POST” API call, without these Cosmosdb Metadata properties mixed in:

“_rid”:”ehszALxRRgACAAAAAAAAAA==”, “_ts”:1408340640, “_self”:”dbs\/ehszAA==\/colls\/ehszALxRRgAALxRRgACAAAAAAAAAA==\/”, “_etag”:”00002500-0000-0000-0000-53f192a00000″, “_attachments”:”attachments\/”

As of now there is no direct way to omit these properties when you are querying the documents. However, cosmosdb team is aware of this feature request, understand the reasons for it, and are considering it for a future release.

For those who are wondering how to omit these system generated properties, you can simply handle this with a User Defined Function.

function stripMeta(doc) {
    var metaProps = ["_rid", "_ts", "_self", "_etag", "_attachments"];    
    var newDoc = {};
    for(var prop in doc) {
        if (metaProps.indexOf(prop) == -1) {
            newDoc[prop] = doc[prop];
        }
    }
    
    return newDoc;
 }

And you can retrieve your documents with whatever queries as follows,

select value udf.stripMeta(c) from c

Hope this helps someone out there.

%d bloggers like this: