def array_add_unique(self, client, key = '', path = '', value = None, xattr=None, create_parents=None): try: new_path = self.generate_path(self.nesting_level, path) if self.is_sdk_client: client.mutate_in(key, SD.array_addunique(new_path, value, xattr=xattr, create_parents=create_parents)) else: client.array_add_unique_sd(key, new_path, json.dumps(value)) except Exception as e: self.log.error(e) self.fail("Unable to add key {0} for path {1} after {2} tries".format(key, path, 1))
def array_add_unique(self, client, key = '', path = '', value = None): try: if self.verbose_func_usage: self.log.info(" array_add_unique ----> {0} :: {1}".format(path, value)) if self.use_sdk_client: client.mutate_in(key, SD.array_addunique(path, value, xattr=self.xattr)) else: client.array_add_unique_sd(key, path, value) except Exception: raise
def array_add_unique(self, client, key='', path='', value=None): try: if self.verbose_func_usage: self.log.info(" array_add_unique ----> {0} :: {1}".format(path, value)) if self.use_sdk_client: client.mutate_in(key, SD.array_addunique(path, value, xattr=self.xattr)) else: client.array_add_unique_sd(key, path, value) except Exception: raise
def array_add_unique(self, client, key = '', path = '', value = None, xattr=None, create_parents=None): try: new_path = self.generate_path(self.nesting_level, path) if self.is_sdk_client: client.mutate_in(key, SD.array_addunique(new_path, value, xattr=xattr, create_parents=create_parents)) else: client.array_add_unique_sd(key, new_path, json.dumps(value)) except Exception as e: self.log.error(e) self.fail("Unable to add key {0} for path {1} after {2} tries".format(key, path, 1))
def test_mutate_in(self): res = None self.bucket.upsert( 'king_arthur', { 'name': 'Arthur', 'email': '*****@*****.**', 'interests': ['Holy Grail', 'African Swallows'] }) with tracer.start_active_span('test'): res = self.bucket.mutate_in( 'king_arthur', SD.array_addunique('interests', 'Cats'), SD.counter('updates', 1)) assert (res) self.assertTrue(res.success) spans = self.recorder.queued_spans() self.assertEqual(2, len(spans)) test_span = get_first_span_by_name(spans, 'sdk') assert (test_span) self.assertEqual(test_span.data["sdk"]["name"], 'test') cb_span = get_first_span_by_name(spans, 'couchbase') assert (cb_span) # Same traceId and parent relationship self.assertEqual(test_span.t, cb_span.t) self.assertEqual(cb_span.p, test_span.s) assert (cb_span.stack) self.assertIsNone(cb_span.ec) self.assertEqual(cb_span.data["couchbase"]["hostname"], "%s:8091" % testenv['couchdb_host']) self.assertEqual(cb_span.data["couchbase"]["bucket"], 'travel-sample') self.assertEqual(cb_span.data["couchbase"]["type"], 'mutate_in')
def sd_array_addunique(self, key, path, val): try: return self._cb.mutate_in( key, SD.array_addunique(path, val, create_parents=True)) except SubdocPathExistsError: return True
# end::add_multi_slow[] # tag::create_parents_array[] collection.upsert("some_doc", {}) collection.mutate_in( "some_doc", [ SD.array_prepend( "some.array", "Hello", "World", create_parents=True)]) # end::create_parents_array[] # tag::array_addunique[] try: collection.mutate_in( "customer123", [ SD.array_addunique( "purchases.complete", 95)]) print('Success!') except PathExistsException: print('Path already exists.') try: collection.mutate_in( "customer123", [ SD.array_addunique( "purchases.complete", 95)]) print('Success!') except PathExistsException: print('Path already exists.') # end::array_addunique[] # cannot add JSON obj w/ array_addunique
[SD.array_append("some.array", "Hello", "World", create_parents=True)]) #end::createparentsarray[] """ ---- == Arrays as Unique Sets Limited support also exists for treating arrays like unique sets, using the _subdoc-array-addunique_ command. This will do a check to determine if the given value exists or not before actually adding the item to the array: [source,csharp] ---- """ #tag::arrayaddunique[] collection.mutate_in("customer123", [SD.array_addunique("purchases.complete", 95)]) # => Success collection.mutate_in("customer123", [SD.array_addunique("purchases.complete", 95)]) # => SubdocPathExists exception! #end::arrayaddunique[] """ ---- Note that currently the _addunique_ will fail with a _Path Mismatch_ error if the array contains JSON _floats_, _objects_, or _arrays_. The _addunique_ operation will also fail with _Cannot Insert_ if the value to be added is one of those types as well. Note that the actual position of the new element is undefined, and that the array is not ordered.