couchbase合并数组到子文档

时间:2018-08-08 06:40:26

标签: python-3.x couchbase

我必须将arry合并到榻榻米基键值数组,我已经读过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]]

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

1 个答案:

答案 0 :(得分: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")