1
votes

Here is the code I use to delete a document:

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

It works for a collection with partition key. For such a case I pass ['myPartitionKey'] for partitionKeys variable. But I am lost for collection that does not use partitioning.

A number of issues and PRs in azure-documentdb-node and vscode-cosmosdb cross reference each other.

What I also did not understand is why instead of fixing documentdb npm package repository the fixes are made in vscode-cosmosdb.

This issue mentions the problem and here possible solution is shared.

Although I tried passing null, undefined and {}, nothing worked. I am getting:

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
Is your collection a partitioned collection and you simply did not provide the partition key value for the document you're trying to delete or the collection is not a partitioned collection? Which one is the case?Gaurav Mantri
@GauravMantri my collection is a small one. I do not use a partitioning for it.Farrukh Normuradov
Then I think passing {} as second parameter should work. This is how we're using in our code. client.deleteDocument(docUrl, {}, (err) => {}).Gaurav Mantri
I tried it once again now, after your comment. It returns the error with the message mentioned in the question above. My documentdb npm package version is 1.14.5.Farrukh Normuradov
@FarrukhNormuradov Hi,any updates now?Jay Gong

1 Answers

2
votes

I did two tests for you. My documentdb npm package version is 1.14.2

First situation: Want to delete document which is not defined partition key in partitioning collection.

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: Want to delete document which is not defined partition key in non-partitioning collection.

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

Hope it helps you.