CosmosDB 存储过程 - 使用分区键进行搜索
CosmosDB stored procedure - use partition key for searching
很简单,我有两个实体,例如:
public class SchoolClass
{
[JsonProperty(PropertyName = "id")]
[Key]
public Guid Id {get; init;} = Guid.NewGuid();
// partition key
public Guid SchoolClassCode {get; init;} = Guid.NewGuid();
public int CountOfComputer {get; set;}
}
和
public class Computer
{
[JsonProperty(PropertyName = "id")]
[Key]
public Guid Id {get; init;} = Guid.NewGuid();
// class where the computer is located (link to SchoolClass class - SchoolClassCode prop)
public Guid SchoolClass {get; set;}
}
我想编写存储过程,这将增加学校计算机的数量 class,一旦一些计算机被添加到数据库中。
我有以下问题:
我不知道如何使用分区键在集合中进行搜索。我需要与这个例子非常相似的东西 https://docs.microsoft.com/en-us/azure/cosmos-db/how-to-model-partition-example#denormalizing-comment-and-like-counts。但是他们使用 id 在集合中进行搜索,我想使用 SchoolClass class (SchoolClassCode 属性) 的分区键,我在 Computer class (SchoolClass 属性 ).我在计算机 class 中没有 SchoolClass class 的 ID,因此我无法找到能够增加计数器的特定 SchoolClass。
所以我需要的是这样的东西:
function createComment(computer) {
var collection = getContext().getCollection();
collection.readDocument(
`${collection.getAltLink()}/docs/${computer.SchoolClass}`,
function (err, schoolClass) {
if (err) throw err;
schoolClass.CountOfComputer++;
collection.replaceDocument(
schoolClass._self,
schoolClass,
function (err) {
if (err) throw err;
}
);
})
}
但 collection.getAltLink()}/docs/ 仅适用于实体 ID,不适用于分区键。我如何使用分区键在集合中搜索以提高性能?谢谢!
您可以像这样使用 queryDocuments
函数:
function createComment(computer) {
var collection = getContext().getCollection();
collection.queryDocuments(
collection.getSelfLink(),
'SELECT * FROM root r where r.SchoolClassCode = ' + computer.SchoolClass,
function (err, items) {
if (err) throw err;
if(items && items.length){
var schoolClass = items[0];
schoolClass.CountOfComputer++;
collection.replaceDocument(
schoolClass._self,
schoolClass,
function (err) {
if (err) throw err;
}
);
}
})
}
很简单,我有两个实体,例如:
public class SchoolClass
{
[JsonProperty(PropertyName = "id")]
[Key]
public Guid Id {get; init;} = Guid.NewGuid();
// partition key
public Guid SchoolClassCode {get; init;} = Guid.NewGuid();
public int CountOfComputer {get; set;}
}
和
public class Computer
{
[JsonProperty(PropertyName = "id")]
[Key]
public Guid Id {get; init;} = Guid.NewGuid();
// class where the computer is located (link to SchoolClass class - SchoolClassCode prop)
public Guid SchoolClass {get; set;}
}
我想编写存储过程,这将增加学校计算机的数量 class,一旦一些计算机被添加到数据库中。
我有以下问题: 我不知道如何使用分区键在集合中进行搜索。我需要与这个例子非常相似的东西 https://docs.microsoft.com/en-us/azure/cosmos-db/how-to-model-partition-example#denormalizing-comment-and-like-counts。但是他们使用 id 在集合中进行搜索,我想使用 SchoolClass class (SchoolClassCode 属性) 的分区键,我在 Computer class (SchoolClass 属性 ).我在计算机 class 中没有 SchoolClass class 的 ID,因此我无法找到能够增加计数器的特定 SchoolClass。
所以我需要的是这样的东西:
function createComment(computer) {
var collection = getContext().getCollection();
collection.readDocument(
`${collection.getAltLink()}/docs/${computer.SchoolClass}`,
function (err, schoolClass) {
if (err) throw err;
schoolClass.CountOfComputer++;
collection.replaceDocument(
schoolClass._self,
schoolClass,
function (err) {
if (err) throw err;
}
);
})
}
但 collection.getAltLink()}/docs/ 仅适用于实体 ID,不适用于分区键。我如何使用分区键在集合中搜索以提高性能?谢谢!
您可以像这样使用 queryDocuments
函数:
function createComment(computer) {
var collection = getContext().getCollection();
collection.queryDocuments(
collection.getSelfLink(),
'SELECT * FROM root r where r.SchoolClassCode = ' + computer.SchoolClass,
function (err, items) {
if (err) throw err;
if(items && items.length){
var schoolClass = items[0];
schoolClass.CountOfComputer++;
collection.replaceDocument(
schoolClass._self,
schoolClass,
function (err) {
if (err) throw err;
}
);
}
})
}