-
Notifications
You must be signed in to change notification settings - Fork 0
Home
daniel edited this page Nov 10, 2025
·
27 revisions
-
範例: (不加 PartitionKey)
直接使用 queryText 查詢
// 初始化分享列表,用於存儲查詢結果
List<Share> shares = new List<Share>();
// 取得教師編碼(必填)
if (!request.TryGetProperty("code", out JsonElement code)) { return BadRequest(); }
// 取得類型(必填):coedit/share
if (!request.TryGetProperty("type", out JsonElement type)) { return BadRequest(); }
await foreach (var item in client
.GetContainer(Constant.TEAMModelOS, "Teacher")
.GetItemQueryIteratorSql<Share>(
queryText: "SELECT * FROM c WHERE STARTSWITH(c.code, \"Share-share-\")"
))
{
shares.Add(item);
}
- 查詢範例
PK 只能用完整、準確、無偏差的值,Cosmos 不陪你模糊、也不會腦補
// 取得必填參數
if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
if (!request.TryGetProperty("type", out JsonElement type)) return BadRequest();
// 初始化分享列表
List<Share> shares = new List<Share>();
// 取得 Cosmos DB 客戶端
var client = _azureCosmos.GetCosmosClient();
// 執行查詢
await foreach (var item in client
.GetContainer(Constant.TEAMModelOS, "Teacher")
.GetItemQueryIteratorSql<Share>(
queryText: "SELECT * FROM c WHERE c.pk = 'Share'",
requestOptions: new QueryRequestOptions()
{
PartitionKey = new PartitionKey($"Share-{type}-{code}") // 使用 PartitionKey 精準定位分區,提高效能
}))
{
shares.Add(item);
}
// 返回結果
return Ok(new { shares = shares });
SQL 選資料內容
QueryRequestOptions 決定查哪個分區
查詢現有容器的分區鍵(Partition key):
在 Data Explorer 點進 Container → Settings → Partition key。
| 服務 | 第一層 | 第二層 | 第三層 | 最底層 |
|---|---|---|---|---|
| Cosmos DB | Account | Database | Container | Item/Document |
| Blob Storage | Storage Account | Container | Blob | – |
使用安全殼層 (SSH) 金鑰連線至 Azure 中的 Linux 虛擬機器 (VM) 的 Windows 使用者。 您也可以在 Azure 入口網站中產生並儲存 SSH 金鑰,以便透過入口網站建立 VM 時使用。