问题描述
我尝试通过 Node.js 应用程序针对 Azure Cosmos DB API for MongoDB 执行 mongoDB 聚合管道。我使用 mongodb
版本的 3.6.2
包连接到云资源。
虽然管道在 Azure 门户中运行没有任何错误,但我在 Node 环境中遇到了错误。
MongoError:此帐户未启用聚合管道。详情请参阅https://aka.ms/mongodb-aggregation。
链接的资源似乎已过时,因为提到的 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();
}
}
您有什么建议,如何解决问题?
解决方法
事实证明,传递给 {% for j in 1..12 %}
<option>{{ '2012-' ~ j ~ '-01' |date('M') }}</option>
{% endfor %}
的 url
与 Azure 门户中的连接字符串不同。
MongoClient.connect()
虽然 // 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}`;
对两个 URL 都有效,但 db().collection().find()
仅对第二个 URL 有效。
最后我在 Azure 文档中找到了正确的命名规则,其中有一个额外的部分 - Azure Cosmos DB's API for MongoDB (3.6 version): supported features and syntax。