Insert Unique JSON data in array

Hi folks,

I want to insert unique JSON data in an array using sub-document API. But it is giving following error.

couchbase.exceptions._SubdocCantInsertValueError_0x44 (generated, catch SubdocCantInsertValueError): <RC=0x44[Subdocument operation would invalidate the JSON], Subcommand failure, Results=1, C Source=(src/callbacks.c,442), OBJ=Spec<ARRAY_ADD_UNIQUE, ‘doc1’, 0, {‘value’: 300, ‘key’: 6}>>
I am trying this way:

cb.mutate_in(‘7’,SD.array_addunique(‘doc1’,{‘key’:6,‘value’:300}))

My sample doc:

“doc1”: [
{
“key”: 1,
“value”: 200
},
{
“key”: 5,
“value”: 100
}
]

I want to insert this into “doc1” array using sub-document API.

  "key": 6,
  "value": 300

Thank you.

A couple of things. First, to clarify, ‘doc1’ is the document id, right?

Looking at the structure, you have

document id: doc1
JSON contents: [{ "key": 1, "value": 200 }, ...]

array_addunique doesn’t support adding objects. That looks to be the main problem. See the notes under the code sample in the section entitled “Arrays as unique sets” on this page:
https://developer.couchbase.com/documentation/server/5.5/sdk/subdocument-operations.html

Do you need the check that values are unique? Otherwise maybe the append/prepend operations could work for you.

Also, looking at the mutate_in call, the first argument should be the document id. I’m not sure where the ‘7’ is coming from.

Hi @hod.greeley

Thank you for your response.

First thing “doc1” is not an id. It is a path of json document.

Yes, I want to check unique values.

‘7’ is document id.

Thank you.