【问题标题】:Delete a document from CosmosDB collection with no partition key using NodeJS使用 NodeJS 从 CosmosDB 集合中删除没有分区键的文档
【发布时间】:2018-07-17 11:44:32
【问题描述】:

这是我用来删除文档的代码:

const CosmosDbClient = require('documentdb').DocumentClient
let client = new CosmosDbClient(URL, {
    masterKey: KEY
  })
client.deleteDocument(docUrl, {
    partitionKey: partitionKeys
  }, (err) => {
    if (err) {
      throw err
    } else {
      console.log('DELETED document ' + docUrl)
    }
  })

它适用于带有分区键的集合。对于这种情况,我将['myPartitionKey'] 传递给partitionKeys 变量。但是我迷失了不使用分区的集合。

azure-documentdb-node 和 vscode-cosmosdb 中的一些问题和 PR 相互交叉引用。

我也不明白为什么不修复 documentdb npm 包存储库,而是在 vscode-cosmosdb 中进行修复。

This issue 提到了问题,并分享了@98​​7654322@ 可能的解决方案。

虽然我尝试传递null, undefined and {},但没有任何效果。我得到:

Partition key provided either doesn't correspond to definition in the collection or doesn't match partition key field values specified in the document.

【问题讨论】:

  • 您的集合是分区集合,而您只是没有为要删除的文档提供分区键值,或者该集合不是分区集合?是哪一种?
  • @GauravMantri 我的收藏很小。我不使用分区。
  • 那么我认为将{} 作为第二个参数传递应该可以。这就是我们在代码中使用的方式。 client.deleteDocument(docUrl, {}, (err) => {}).
  • 在您发表评论后,我现在又试了一次。它返回带有上述问题中提到的消息的错误。我的 documentdb npm 包版本是1.14.5
  • @FarrukhNormuradov 嗨,现在有更新吗?

标签: node.js azure azure-cosmosdb


【解决方案1】:

我为你做了两个测试。我的 documentdb npm 包版本是1.14.2

第一种情况:要删除分区集合中未定义分区键的文档。

示例文档:

删除代码:

var config = require("./config");

var docUrl= "dbs/db/colls/coll/docs/3"

const CosmosDbClient = require('documentdb').DocumentClient
let client = new CosmosDbClient(config.endpoint, {
    masterKey: config.primaryKey
  })
client.deleteDocument(docUrl, {
    partitionKey: {}
  }, (err) => {
    if (err) {
      console.log(err)
      throw err
    } else {
      console.log('DELETED document ' + docUrl)
    }
  })

第二种情况:要删除非分区集合中未定义分区键的文档。

示例文档:

删除代码:

var config = require("./config");

var docUrl= "dbs/db/colls/import/docs/3"

const CosmosDbClient = require('documentdb').DocumentClient
let client = new CosmosDbClient(config.endpoint, {
    masterKey: config.primaryKey
  })
client.deleteDocument(docUrl, {
  }, (err) => {
    if (err) {
      console.log(err)
      throw err
    } else {
      console.log('DELETED document ' + docUrl)
    }
  })

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-10
    • 1970-01-01
    • 2020-11-11
    • 1970-01-01
    相关资源
    最近更新 更多