arrayAddUnique comparison logic

How arrayAddUnique compare items , How couchbase compare objects and arrays?

Is {"a":1,"b":2} equal to {"a":1,"b":2} , what about {"b":2,"a":1}
Is [1,2] equal to [1,2] , what about [2,1]

In the N1QL

  • array (element by element comparison is performed until the end of the shorter array; if all the elements so far are equal, then longer arrays sort after)
  • object (larger objects sort after; for objects of equal length, key/value by key/value comparison is performed; keys are examined in sorted order using the normal ordering for strings)

https://docs.couchbase.com/server/5.5/n1ql/n1ql-language-reference/orderby.html

Also Try this.

 SELECT  {"a":1,"b":2}  == {"a":1,"b":2} AS `{"a":1,"b":2}  == {"a":1,"b":2}`,
                  {"a":1,"b":2}  == {"b":2,"a":1} AS `{"a":1,"b":2}  == {"b":2,"a":1}`,
                  [1,2] == [1,2] AS `[1,2] == [1,2]`,
                  [1,2] == [2,1] AS `[1,2] == [2,1]`;

If you are not interested in array position, you can use ARRAY_SORT(ARRAY_DISTINCT( a1)) == ARRAY_SORT(ARRAY_DISTINCT( a2)) .

1 Like

Is n1ql comparison logis equal to bucket.mutateIn.arrayAddUnique logic?

Can you explain bold statements , I don’t understand

  • array (element by element comparison is performed until the end of the shorter array; if all the elements so far are equal, then longer arrays sort after)
  • object (larger objects sort after; for objects of equal length, key/value by key/value comparison is performed; keys are examined in sorted order using the normal ordering for strings)
[1,2,3],  [1,2] [1,2,3,4]  If sorted ascending    [1,2] , [1,2,3], [1,2,3,4]
If you look first two arrays first element is same then shorter array comes first and longer array goes after.

Same for object.
{"a":1, "b":2, "c":3}, {"a":1, "b":2:} , {"a":1, "b":2, "c":3, "d":4}
 If sorted ascending    {"a":1, "b":2:}, {"a":1, "b":2, "c":3},  {"a":1, "b":2, "c":3, "d":4}
1 Like