问题
I want to do an "uppend_multi": an append_multi
(http://pythonhosted.org/couchbase/api/couchbase.html#couchbase.bucket.Bucket.append_multi) where some of the keys may not exist. If they do not already exist then the append operation should be an insert. How can I do this? Nothing in the API docs suggests this is supported, but I imagine it is a very common operation.
Right now I am doing this, but it seems so natural of a thing to need to do that I highly doubt this is the best way to do it:
def _uppend_multi(bucket, append_dict):
reinsert_dict = {}
try:
bucket.append_multi(append_dict, format = couchbase.FMT_UTF8)
except CouchbaseError as exc:
for k, res in exc.all_results.items():
if res.success:
pass
else:
reinsert_dict[k] = append_dict[k]
if len(reinsert_dict.keys()) > 0:
bucket.insert_multi(reinsert_dict, format = couchbase.FMT_UTF8)
回答1:
As far as I know there is no such "uppend" operation in the Couchbase Python SDK, nor in the lower-level C API (libcouchbase
).
What you're currently doing is probably a pretty reasonable solution.
来源:https://stackoverflow.com/questions/32866825/couchbase-python-sdk-uppend