Stored procedures CosmosDB存储过程-使用分区键进行搜索

Stored procedures CosmosDB存储过程-使用分区键进行搜索,stored-procedures,azure-cosmosdb,Stored Procedures,Azure Cosmosdb,很容易说,我有两个实体,比如: public class SchoolClass { [JsonProperty(PropertyName = "id")] [Key] public Guid Id {get; init;} = Guid.NewGuid(); // partition key public Guid SchoolClassCode {get; init;} =

很容易说,我有两个实体,比如:

    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;}
    }

我想写一个存储过程,一旦一些计算机被添加到数据库中,它将增加学校课堂上的计算机数量

我有以下问题: 我不知道,如何使用分区键在集合中搜索。我需要一些与这个例子非常相似的东西。但是他们使用id在集合中搜索,我想使用SchoolClass类的分区键(SchoolClassCode属性),这是我在计算机类(SchoolClass属性)中拥有的。我没有计算机课的班级id,因此我无法找到特定的班级来增加计数器

所以我需要的是这样的东西:

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;
        }
      );
    })
}
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;
            }
        );
      }
    })
}

但是collection.getAltLink()}/docs/仅适用于实体id,而不适用于分区键。如何使用分区键在集合中搜索,从而提高性能?谢谢

您可以像这样使用
queryDocuments
函数:

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;
        }
      );
    })
}
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;
            }
        );
      }
    })
}

谢谢,出于某种原因,我想避免在存储过程中使用查询,但我可以看到,即使是MS也在文档中使用它,所以我希望它干净且性能安全。。谢谢你的帮助!