arrayAddUnique "Subdocument operation would invalidate the JSON"


#1

Why the following code returns error Subdocument operation would invalidate the JSON

bucket.mutateIn('key')
.arrayAddUnique('myArray',{foo:'bar'}, {createParents: true})
.execute(function(err,result){
  result.content('myArray')
});

key exists but has no myArray field , so I expect to create an array and add object to it
Why the result can be invalid JSON ?


#2

I found that arrayAddUnique is not support objects , Is there alternative way?
I have an array of objects and I want to keep object unique , at least by id field of object


#3

Hey @socketman2016,

Unfortunately I do not believe it is possible to have an array of unique objects as object comparison is a non-trivial task. You would need to perform a CAS swap in this case in order to perform the operation. You can use CAS in tandem with Subdoc to reduce the amount of data going across the network by performing a subdocument fetch of the array, filtering it for insertion, and then a subdocument mutation with CAS.

Cheers, Brett