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

时间:2017-08-03 17:33:32

标签: go arangodb

我尝试使用用户定义的密钥创建文档,如下所示:

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'

我已经尝试使用doc类型的属性作为' _key',' key'和' Key'。没有人工作过。

1 个答案:

答案 0 :(得分:3)

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

同时,DB期望JSON文档包含_key属性。

所以你应该把它指定为:

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

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

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