我正在尝试使用用户定义的键创建文档,如下所示:
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())
}
Run Code Online (Sandbox Code Playgroud)
我得到以下输出:
Created document with key '5439648' in collection 'collname'
Run Code Online (Sandbox Code Playgroud)
我尝试过将文档类型的属性设置为“_key”、“key”和“Key”。没有一个有效。
该字段需要可见(大写)才能包含在 JSON 编组中。
同时,数据库期望 JSON 文档包含属性_key。
所以你应该将其指定为:
type doc struct {
Key string `json:"_key"`
}
Run Code Online (Sandbox Code Playgroud)
或者,您可以尝试map向该方法发送:
coll.CreateDocument(nil, map[string]string{"_key": "mykey"})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
465 次 |
| 最近记录: |