首页 文章

使用NodeJS从没有分区键的CosmosDB集合中删除文档

提问于
浏览
1

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

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包存储库而不是修复documentdb npm包存储库 .

This问题提到了问题并且here可能的解决方案是共享的 .

虽然我试过传递 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.

1 回答

  • 1

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

    First situation: 想删除分区集合中未定义分区键的文档 .

    sample documents:

    enter image description here

    delete code:

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

    enter image description here

    Second situation: 想删除非分区集合中未定义分区键的文档 .

    sample documents:

    enter image description here

    delete code:

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

    enter image description here

    希望它能帮到你 .

相关问题