3
votes

It's really neat that Azure Cosmos DB now also supports Aggregation Pipelines, and this makes it a viable replacement for us to use instead of running our own Mongo DB Containers, but I have failed to find a way to enable the features via code (how to do it in the Portal is described here: https://azure.microsoft.com/en-gb/blog/azure-cosmosdb-extends-support-for-mongodb-aggregation-pipeline-unique-indexes-and-more/).

We need this for the integration and testing environments which we create via deployment pipelines from scratch every day, and the backing Cosmos DB instances must support Aggregation pipelines.

I have checked the API documentation at https://docs.microsoft.com/en-us/rest/api/documentdb/, and also the az cosmosdb command line tool, but I can't find the right setting to pass in.

Has this just not yet surfaced, or am I missing something?

2
Currently, this functionality is only available via Azure Portal. Azure CLI support for enabling preview features will be added soon. I will share a link to the documentation once this is liveSiddhesh Vethe
@SiddheshVethe OK, thanks. This would be a great thing to have.donmartin
Any news on this, @SiddheshVethe? This is getting annoying, unfortunately.donmartin

2 Answers

4
votes

You can track the change with the following pull request: https://github.com/Azure/azure-cli/pull/5451#pullrequestreview-94854631

The following command will allow you to enable aggregation pipeline.

az cosmosdb update -n {acc} -g {rg} --capabilities EnableAggregationPipeline
2
votes

DON'T DO THIS - see accepted answer.

A colleague of mine found the following temporary solution for this problem, using a presumably undocumented API of Azure (this is a bash script). Pass in LOCATION, RESOURCE_GROUP and BM_ACCOUNT, and this script will create a Mongo API Cosmos DB account with enabled aggregation pipelines.

TOKEN=$(az account get-access-token | jq ".accessToken" | tr -d '"')

if [ -z "$LOCATION" ]; then
    export LOCATION="NorthEurope"
fi
echo "INFO [cosmos]: Using location: $LOCATION"
echo "INFO [cosmos]: Creating bookmarks DB"
BM_ACCOUNT="name-of-your-bookmark-db"
az cosmosdb create --resource-group $RESOURCE_GROUP \
    --name $BM_ACCOUNT \
    --kind MongoDB \
    --locations "$LOCATION=0"

curl -X PATCH \
     -H "Authorization: Bearer ${TOKEN}" \
     -H 'Content-Type: application/json' \
     --data '{"properties":{"capabilities":[{"name":"EnableAggregationPipeline","description":null}]}}' \
     "https://management.azure.com/subscriptions/${SUBSCRIPTION_ID}/resourceGroups/${RESOURCE_GROUP}/providers/Microsoft.DocumentDb/databaseAccounts/${BM_ACCOUNT}/?api-version=2015-04-08" 

WAIT_FOR=12
SUCCESS=0
while [ $WAIT_FOR -gt 0 ]; do
    sleep 10

    RESULT=$(curl -H "Authorization: Bearer ${TOKEN}" \
                  -H 'Content-Type: application/json' \
                  "https://management.azure.com/subscriptions/${SUBSCRIPTION_ID}/resourceGroups/${RESOURCE_GROUP}/providers/Microsoft.DocumentDb/databaseAccounts/${BM_ACCOUNT}/?api-version=2015-04-08" \
                  | jq ".properties.capabilities[].name" \
                  | tr -d '"')

    if [ "$RESULT" == "EnableAggregationPipeline" ]; then
        SUCCESS=1
        break;
    fi

    echo "INFO [cosmos]: Waiting another ${WAIT_FOR} tries for 'EnableAggregationPipeline' capability..."
    ((WAIT_FOR--))
done

if [ $SUCCESS -eq 0 ]; then
    echo "ERROR [cosmos]: Did not get required CosmosDB capability of 'EnableAggregationPipeline' in time for account ${BM_ACCOUNT} - giving up." >&2
    exit 1
fi

I perhaps wouldn't recommend using this for production purposes, but as far as we see, it actually works.