Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用golang更新firestore中的数组元素?_Go_Google Cloud Firestore - Fatal编程技术网

如何使用golang更新firestore中的数组元素?

如何使用golang更新firestore中的数组元素?,go,google-cloud-firestore,Go,Google Cloud Firestore,以下代码: Lobby := map[string]interface{}{ "table_id" :"new table id", "Status" : true, "name" : "shivam", "array":[]interface{}{0,1,3},// this replace existing array with new values }

以下代码:

Lobby := map[string]interface{}{
        "table_id"          :"new table id",
        "Status"            : true,
        "name"              : "shivam",
        "array":[]interface{}{0,1,3},// this replace existing array with new values
    }

result, err := client.Collection("lobbies").Doc("12").Set(ctx,Lobby,firestore.MergeAll)

我只想用新值更新数组中的第二个元素,但无法通知数据库替换切片或数组中的特定元素

您的
数组
字段存储一个切片,而不是映射,因此您需要实现自己的代码,以您需要的方式重新生成该切片并在文档中替换它,例如:

Lobby := map[string]interface{} {
    "table_id" : "new table id",
    "Status"   : true,
    "name"     : "shivam",
    "array"    : []interface{}{0,1,3},
}

new_slice, err := change_my_slice(Lobby["array"])
if err != nil {
    log.Errorf("Error message goes here")
    return nil, err
}
Lobby["array"] = new_slice

result, err := client.Collection("lobbies").Doc("12").Set(ctx,Lobby,firestore.MergeAll)

无法通知数据库替换切片或数组中的特定元素

您的
数组
字段存储一个切片,而不是映射,因此您需要实现自己的代码,以您需要的方式重新生成该切片并在文档中替换它,例如:

Lobby := map[string]interface{} {
    "table_id" : "new table id",
    "Status"   : true,
    "name"     : "shivam",
    "array"    : []interface{}{0,1,3},
}

new_slice, err := change_my_slice(Lobby["array"])
if err != nil {
    log.Errorf("Error message goes here")
    return nil, err
}
Lobby["array"] = new_slice

result, err := client.Collection("lobbies").Doc("12").Set(ctx,Lobby,firestore.MergeAll)

仅供参考:您当前的代码正在替换文档,而不是更新文档。要在不覆盖整个文档的情况下更新文档的某些字段,请使用该方法。@AndrejsCainikovs thanx获得回复。根据谷歌文档,我正在使用mergeall方法更新上述字段。文档中还有很多字段不想更新,所以这里就不提了。如果我理解正确,这与Firestore和数据库一般无关,你的问题是如何替换切片中的第二个元素?@AndrejsCainikovs。不,在firestore数据库中,我有一个文档,它有一个名为“数组”(作为数组类型)的字段,现在有3个值0,2,3,4,5,6,我想用0,1,3替换前3个值,即0,2,3。但当我这样做时,它会重新放置数组中的所有值。当然,它会被覆盖<代码>数组字段存储一个切片,而不是映射,因此您需要实现自己的代码,以按照需要的方式重新生成该切片并在文档中替换它。仅供参考:您当前的代码正在替换文档,而不是更新文档。要在不覆盖整个文档的情况下更新文档的某些字段,请使用该方法。@AndrejsCainikovs thanx获得回复。根据谷歌文档,我正在使用mergeall方法更新上述字段。文档中还有很多字段不想更新,所以这里就不提了。如果我理解正确,这与Firestore和数据库一般无关,你的问题是如何替换切片中的第二个元素?@AndrejsCainikovs。不,在firestore数据库中,我有一个文档,它有一个名为“数组”(作为数组类型)的字段,现在有3个值0,2,3,4,5,6,我想用0,1,3替换前3个值,即0,2,3。但当我这样做时,它会重新放置数组中的所有值。当然,它会被覆盖<代码>数组字段存储一个切片,而不是一个映射,所以您需要实现自己的代码,以您需要的方式重新生成该切片,并在文档中替换它。