node.js - 该帐户未启用聚合管道

标签 node.js mongodb azure azure-cosmosdb azure-cosmosdb-mongoapi

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

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

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

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

(我的管道包含 $group 表达式。如果没有 $group,它会返回结果。)

我猜,问题与我的客户端设置有关,因为它在 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 门户中的连接字符串不同。 p>

// 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() 适用于两个 URL,但 db().collection().aggregate() 仅适用于第二个 URL一个。

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

关于node.js - 该帐户未启用聚合管道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65406756/

相关文章:

azure - 使用 Azure 网站,但 Web 邮件解决方案怎么样

.net - 使用 AMS v3 克隆 Azure 媒体服务 Assets /视频

javascript - MongoDB 事务有很多 WriteConflict 错误

node.js - 访问 Amazon Alexa 控制台 console.log() 内容

javascript - 使用 Lodash 进行分组和计数

python - 如何初始化UnorderedBulkOp()?

mongodb - mongodb db 中的解析错误,插入到具有唯一索引的集合

javascript - Node.js 应用程序在本地正常工作,但在 Droplet Ubuntu 上出现 404 错误

javascript - mongoDB:获取数组对象的字段值

azure - AKS 模板创建新资源组