普通网友 2017-08-03 17:33
浏览 43
已采纳

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

I'm attempting to create a document with a user-defined key as follows:

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'
", meta.Key, coll.Name())
}

I get the following output:

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

I've tried with the property of the doc type as '_key', 'key' and 'Key'. None have worked.

  • 写回答

1条回答 默认 最新

  • duanliao3826 2017-08-03 18:29
    关注

    The field needs to be visible (so uppercase) in order to be included in the JSON marshalling.

    At the same time, the DB expects the JSON document to contain a _key attribute.

    So you should specify it as:

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

    Alternatively, you can try sending a map to the method:

    coll.CreateDocument(nil, map[string]string{"_key": "mykey"})
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件
  • ¥15 使用ESP8266连接阿里云出现问题