【问题标题】:couchbase merge array to sub-documentcouchbase 合并数组到子文档
【发布时间】:2019-01-15 07:53:36
【问题描述】:

我必须合并一个arry to couchbase key-value array,我已经阅读了document,只有这个:

from couchbase.cluster import Cluster,PasswordAuthenticator
import couchbase.subdocument as SD
#other thing
bucket.upsert("all_goods",[])
bucket.mutate_in("all_goods",SD.array_append("",["a","b","c"])
bucket.mutate_in("all_goods",SD.array_append("",["1","2","3"])

我希望得到那个

all_goods => [a,b,c,1,2,3]

但是,我明白了:

all_goods => [[a,b,c],[1,2,3]]

我希望将数组合并到文档中

【问题讨论】:

    标签: python-3.x couchbase


    【解决方案1】:

    根据documentation,这是预期的行为:

    bucket.mutate_in('my_array', SD.array_append('', ['elem1', 'elem2', 'elem3'])
    # the document my_array is now ["some_element", ["elem1", "elem2", "elem3"]]
    

    作为一种解决方法,我建议创建一个数组并extend 它,然后再执行array_append

    from couchbase.cluster import Cluster,PasswordAuthenticator
    import couchbase.subdocument as SD
    #other thing
    bucket.upsert("all_goods",[])
    my_array = ["a","b","c"]
    my_array.extend(["1","2","3"])
    bucket.mutate_in("all_goods",SD.array_append("",my_array)
    

    或者您可以单独附加元素:

    from couchbase.cluster import Cluster,PasswordAuthenticator
    import couchbase.subdocument as SD
    #other thing
    bucket.upsert("all_goods",[])
    bucket.mutate_in("all_goods",SD.array_append("","a","b","c")
    bucket.mutate_in("all_goods",SD.array_append("","1","2","3")
    

    【讨论】:

    • 是的,这是个好主意,但是,我必须 upsert 一个大文档(大约 18.4 MB),一次 upsert 会导致超时错误,数组的长度太长,我必须多次插入它
    • 在这种情况下,我建议您查看文档并检查是否有可以帮助您的内容...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-17
    • 1970-01-01
    • 2016-05-14
    • 2021-08-23
    • 2015-08-22
    • 1970-01-01
    • 2023-04-05
    相关资源
    最近更新 更多