Go 在文档中创建用户定义的键
我正试图创建一个具有用户定义键的文档,如下所示:Go 在文档中创建用户定义的键,go,arangodb,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.C
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类型的属性设置为“\u key”、“key”和“key”。没有一个有效。该字段需要可见(大写),以便包含在JSON编组中 同时,DB希望JSON文档包含一个
\u key
属性
因此,您应该将其指定为:
type doc struct {
Key string `json:"_key"`
}
或者,您可以尝试将映射发送到以下方法:
coll.CreateDocument(nil, map[string]string{"_key": "mykey"})
任何其他包(包括json
)都必须使用键才能看到它。未报告的字段将永远不会被序列化。太好了,非常感谢。也许我应该在arango docs的路上做个公关。