0
votes

I try to execute a mongoDB aggregation pipeline against Azure Cosmos DB API for MongoDB via a Node.js application. I use the mongodb package with version 3.6.2 to connect to the cloud ressource.

While the pipeline runs without any error in the Azure Portal, I got an error within the Node environment.

MongoError: The aggregation pipeline is not enabled for this account. Please see https://aka.ms/mongodb-aggregation for details.

The linked resource seems outdated, because the mentioned Azure Portal entries don't exist anymore.

(My pipeline contains a $group expression. Without the $group, it returns a result.)

I guess, the problem is related to my client setup, since it works within the Mongo Shell.

async function aggregate<T>(pipeline: Record<string, unknown>[]): Promise<T[]> {
  const client = await MongoClient.connect(COSMOSDB_URL), {
    useUnifiedTopology: true,
    auth: { user: COSMOSDB_ACCOUNT_NAME, password: COSMOSDB_PASSWORD }
  });
  try {
    // Returns the error
    return await client.db().collection('coll').aggregate<T>(pipeline).toArray();
  } finally {
    await client.close();
  }
}

Do you have any suggestions, how to fix the issue?

1
Seems a similar issue as described in the azure-docs repository - github.com/MicrosoftDocs/azure-docs/issues/45723sschmeck

1 Answers

1
votes

It turned out, that the url passed to MongoClient.connect() differed from the Connection String in the Azure Portal.

// Returns error "The aggregation pipeline is not enabled for this account". 
const COSMOSDB_URL = `mongodb://${account_name}.documents.azure.com:10255/${database_name}`;

// Correct URL taken from the Connection String (HOST) in the Azure Portal
const COSMOSDB_URL = `mongodb://${account_name}.mongo.cosmos.azure.com:10255/${database_name}`;

While db().collection().find() works for both URLs, db().collection().aggregate() works only for the second one.

Finally I found the correct naming rule in the Azure documentation, where it got an extra section - Azure Cosmos DB's API for MongoDB (3.6 version): supported features and syntax.