无法在Cosmos DB存储过程中检索文档

时间:2020-09-18 12:14:44

标签: javascript stored-procedures azure-cosmosdb

我正在尝试通过JavaScript存储过程中的简单SQL查询来检索与ID匹配的文档。但是它总是返回没有文档与找到的ID相匹配。尝试了Microsoft文档中的所有可用内容以及其他StackOverflow答案。这是我的功能:

function updateDocument () {
  let context = getContext();
  let collection = context.getCollection();
  let collectionLink = collection.getSelfLink();
  let response = context.getResponse();

  tryQueryAndUpdate();

  function tryQueryAndUpdate () {
    var filterQuery = 'select * from user r WHERE r.id = "f5eddf33-7826-40c3-a634-9c349e5c7f73"';

    let isAccepted = collection.queryDocuments(collectionLink, filterQuery, {}, function (err, documents, responseOptions) {
      if (err) throw err;

      if (documents.length > 0) {
        console.log("Documents exists - " + documents.length);
      } else {
        // Else a document with the given id does not exist..
        console.log("Document doesn't exist " + documents.length);
      }
    });
  }
}

我的文档:

{
    "firstName": "firstname",
    "lastName": "lastname",
    "email": "firstname.lastname@gmail.com",
    "businessRole": "",
    "functionalArea": null,
    "type": "internal",
    "status": "enable",
    "modifiedDate": "2020-07-10T14:34:54.557Z",
    "id": "f5eddf33-7826-40c3-a634-9c349e5c7f73",
    "_rid": "H+pKAKZdGd0BAAAAAAAAAA==",
    "_self": "dbs/H+pKAA==/colls/H+pKAKZdGd0=/docs/H+pKAKZdGd0BAAAAAAAAAA==/",
    "_etag": "\"730103dd-0000-0100-0000-5f6302800000\"",
    "_attachments": "attachments/",
    "securityGroup": "c5d3dc10-bfa0-11ea-8394-48463089baf6",
    "engagementId": "945acacf-a3ea-43ef-8e31-70576055ba4f",
    "sort": {
        "firstName": "firstname",
        "lastName": "lastname"
    },
    "userStatus": "active",
    "_ts": 1600324224
}

document: container:

任何帮助将不胜感激。谢谢。

执行参数:

Execution parameters

控制台输出:

Console output

1 个答案:

答案 0 :(得分:0)

正如David在推荐中提到的那样,您的分区键值错误。

您需要使用分区键的值而不是/type