1

I want to update specific fields in below document stored in Couchbase :

 "name": "Douglas Reynholm",
  "email": "douglas@reynholmindustries.com",
  "addresses": {
    "billing": {
      "line1": "123 Any Street",
      "line2": "Anytown ",
      "country": "United Kingdom"
    },
    "delivery": {
      "line1": "123 Any Street",
      "line2": "Anytown ",
      "country": "United Kingdom"
    }
  },
  "purchases": {
    "complete": [
      339, 976, 442, 666
    ],
    "abandoned": [
      157, 42, 999
    ]
  }
}

How can I write a generic query using Java SDK to handle all possible combinations of subdocument update ? For example one user wants to update "addresses.delivery.country" field and other user wants to update "name" field.

It would be tough to right customized query for each possible case of subdocument update.. is that correct ?

Mayank
  • 11
  • 2
  • Do you want to write a N1QL query or use the subdocument API? If the latter, which language are you coding in? – Matthew Groves Jan 29 '20 at 20:15
  • @MatthewGroves - We are using Java. Currently we are loading the entire couchbase document in application to update specific fields. Rather we want to write a single generic query that can handle any type of sub-document updates in couchbase using subdocument API. We dont want to fetch the entire document in application as we have frequent updates. – Mayank Feb 01 '20 at 17:48
  • It looks like you're pulling that example document straight from the subdocument documentation here: https://docs.couchbase.com/java-sdk/2.7/subdocument-operations.html - so I'm confused. There are a number of subdocument methods that allow you to pass in a string parameter, so you can just use that to update any fields you want, individually. Using the subdocument API means that you WON'T be passing the entire document back and forth, just the parts you want to update. – Matthew Groves Feb 03 '20 at 14:23
  • I took that example document to explain the issue i am facing. The question was - an end user can request to update any field of this document. How can i write just a single generic query to handle all cases ? – Mayank Feb 09 '20 at 15:39
  • To put it in other words : I have a this document that contains many fields and I want to update the one or few fields without affecting remaining fields.But I dont know which fields will be selected to update, and I think it is not a good idea to write every query for every scenario. I am looking for a better way to update the document dynamically. – Mayank Feb 09 '20 at 15:41
  • This sounds a little risky to me: allowing a user to make any update they want. But the subdocument path is a string, so you could hypothetically just allow the user to enter a subdocument path and use it directly: `.upsert(subDocPath, yourValue)` for instance. – Matthew Groves Feb 09 '20 at 19:18
  • @MatthewGroves Thanks for answering my question! Just want to share that we are using Rest API built in Java springboot. For example, User will send below payload in PUT api to update just billing country :
    "addresses": { "billing": { "country": "United Kingdom" }, }
    Next time User will send below payload in PUT api to update just email:
    "email": "newemail@gmail.com",
    I should be able to handle all such cases with generic query. Thats the aim.
    – Mayank Feb 15 '20 at 09:27

0 Answers0