我尝试针对 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/