在文档中创建用户定义的键

Creating user-defined keys in document

我正在尝试使用用户定义的键创建文档,如下所示:

package main

import (
    "fmt"
    driver "github.com/arangodb/go-driver"
    "github.com/arangodb/go-driver/http"
)

type doc struct {
    _key string `json:"_key"`
}

func main() {
    conn, _ := http.NewConnection(http.ConnectionConfig{
        Endpoints: []string{"http://localhost:8529"},
    })

    c, _ := driver.NewClient(driver.ClientConfig{
        Connection: conn,
        Authentication: driver.BasicAuthentication("root", "test"),
    })

    db, _ := c.CreateDatabase(nil, "dbname", nil)

    // delete the collection if it exists; then create it
    options := &driver.CreateCollectionOptions{
        KeyOptions: &driver.CollectionKeyOptions{
            AllowUserKeys: true,
        },
    }
    coll, _ := db.CreateCollection(nil, "collname", options)

    meta, _ := coll.CreateDocument(nil, doc{ _key: "mykey" })

    fmt.Printf("Created document with key '%s' in collection '%s'\n", meta.Key, coll.Name())
}

我得到以下输出:

Created document with key '5439648' in collection 'collname'

我已经尝试将文档类型的 属性 作为“_key”、'key' 和 'Key'。 None 成功了。

该字段需要可见(大写)才能包含在 JSON 编组中。

同时,数据库希望 JSON 文档包含 _key 属性。

因此您应该将其指定为:

type doc struct {
    Key string `json:"_key"`
}

或者,您可以尝试向方法发送 map

coll.CreateDocument(nil, map[string]string{"_key": "mykey"})