在cosmosdb mongodb api中自动缩放



"我正在使用azure cosmosdb和mongodb api,我想知道如何检查azure cosmos db mongo api中的数据库是否使用cosmos连接字符串启用了自动缩放。我使用express.js作为中间件框架">

获取此信息的一种可能方法是使用@azure/cosmosNode包。

import {CosmosClient} from '@azure/cosmos';
const endpoint = 'https://youraccountname.documents.azure.com';
const key = 'your account password';
const client = new CosmosClient({endpoint, key});
const databaseName = 'your database name';
const db = client.database(databaseName);
db.readOffer()
.then((response) => {
const offerDetails = response.resource;//This will have all the necessary information.
})
.catch((error) => {
console.log(error);
});

以下是其中一个具有自动缩放吞吐量模式的数据库的响应:

{
"resource":"dbs/XXXXXX==/",
"offerType":"Invalid",
"offerResourceId":"XXXXXX==",
"offerVersion":"V2",
"content":{
"offerThroughput":400,
"offerMinimumThroughputParameters":{
"maxThroughputEverProvisioned":4000,
"maxConsumedStorageEverInKB":0
},
"offerAutopilotSettings":{
"tier":0,
"maximumTierThroughput":0,
"autoUpgrade":false,
"autoUpgradePolicy":{
"throughputPolicy":{
"incrementPercent":10
}
},
"maxThroughput":4000
}
},
"id":"XXxX",
"_rid":"XXxX",
"_self":"offers/XXxX/",
"_etag":""xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"",
"_ts":1625655292
}

您需要做的是检查content下的offerAutopilotSettings属性。只有当为数据库配置的吞吐量模式为"自动缩放"时,才会出现这种情况

var connection = mongoose.createConnection(
connectionString
(error, doc) => {
if (error) {
return res
.json({message: error})             }
}
);
connection.db.command({customAction: "GetDatabase"}) 
.then((data) => {res.json(data)})
.catch(error => {res.json(error)})
Output:
------
{
"database" : "test",
"provisionedThroughput" : 2000,
"autoScaleSettings" : {
"maxThroughput" : 20000
},
"ok" : 1
}

参考:https://learn.microsoft.com/en-us/azure/cosmos-db/mongodb-custom-commands#create-数据库,用于我们可以在comsos DB中使用Mongo API 执行的所有其他功能

最新更新