此帐户未启用聚合管道

The aggregation pipeline is not enabled for this account

我尝试通过 Node.js 应用程序针对 Azure Cosmos DB API for MongoDB 执行 mongoDB 聚合管道。我使用版本为 3.6.2mongodb 包连接到云资源。

虽然管道在 Azure 门户中运行没有任何错误,但我在节点环境中遇到错误。

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

链接的资源似乎已过时,因为提到的 Azure 门户条目不再存在。

(我的管道包含一个 $group 表达式。没有 $group,结果是 returns。)

我想,问题与我的客户端设置有关,因为它在 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();
  }
}

你有什么建议,如何解决这个问题?

事实证明,传递给 MongoClient.connect()url 与 Azure 门户中的 连接字符串 不同。

// 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}`;

虽然 db().collection().find() 适用于两个网址,但 db().collection().aggregate() 仅适用于第二个网址。

我终于在 Azure 文档中找到了正确的命名规则,其中有一个额外的部分 - Azure Cosmos DB's API for MongoDB (3.6 version): supported features and syntax