Farrukh Normuradov
Farrukh Normuradov

Reputation: 1194

Delete a document from CosmosDB collection with no partition key using NodeJS

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.

Upvotes: 1

Views: 2025

Answers (1)

Jay Gong
Jay Gong

Reputation: 23767

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.

Upvotes: 2

Related Questions