cloudant: update document which replace existing data

431 Views Asked by At

I have following document

{
  "_id": "9036472948305957379",
  "_rev":"162de87a696361533791aa7",
  "firstname":"xyz",
  "lastname": "abc"
}

Now I want to update above dosument to following

   {
     "_id": "9036472948305957379",
     "_rev":"162de87a696361533791aa7",
     "name":"xyz abc"
  }

if I do doc['name'] = "xyz abc" it doesnt remove firstname and lastname attributes. how do I achieve that?

1

There are 1 best solutions below

0
ptitzler On BEST ANSWER

You need to explicitly remove the firstname and lastname properties from your local copy of the document before saving it back in the database.

If I understand your issue correctly you are currently sending the following document body (implicitly or explicitly) to the database when you initiate the update operation:

{
 "_id": "9036472948305957379",
 "_rev":"162de87a696361533791aa7",
 "firstname":"xyz",
 "lastname": "abc",
 "name":"xyz abc"
}

However, your payload needs to look as follows:

{
 "_id": "9036472948305957379",
 "_rev":"162de87a696361533791aa7",
 "name":"xyz abc"
}

If you are using the python-cloudant library take a look at the field_set method at http://python-cloudant.readthedocs.io/en/latest/document.html:

static field_set(doc, field, value)

Sets or replaces a value for a field in a locally cached Document object. To remove the field set the value to None.