go - 如何使用 cursor.ReadDocument() 从 arangodb 读取任意对象

标签 go arangodb

我正在尝试构建一个相当通用的服务来在 arangodb 中创建图表。

在我的 ReadVertex() 函数中,我希望能够读取一个顶点的任意字段的属性并将其读入一个变量,最好是一个字符串,而不必创建特定的结构使用针对特定属性进行硬编码的 json 标记。

我可以毫无问题地插入任意对象作为顶点的属性,并且我已经尝试将它们读入字符串,但它总是空白。

一个我想完成的例子:如果一个顶点有一个属性:"Prop1": {"Nested1": true, "Nested2": false},和一个属性"Prop2": "Just a string",在我的 ReadVertex() 函数中,我希望能够按照以下方式做一些事情

// query the database and get a cursor containing prop1 and prop2

var prop1 someType
cursor.ReadDocument(ctx, &prop1)
fmt.Println(prop1.String()) // should print {"Prop1": {"Nested1": true, "Nested2": false}}

var prop2 someType
cursor.ReadDocument(ctx, &prop2)
fmt.Println(prop2.String()) // should print {"Prop2": "Just a string"}

我希望 prop1 和 prop2 的类型相同。

最佳答案

事实证明,答案比我想象的要简单得多。如果 prop1prop2interface{} 类型,它会以映射的形式读取数据,然后只要map 都是字符串(在我的例子中是这样),要将其转换为 json 字符串,我可以使用 json.Marshal() 并将返回的字节数组转换为字符串。

关于go - 如何使用 cursor.ReadDocument() 从 arangodb 读取任意对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56045516/

相关文章:

.Net 中的 ArangoDB 更新操作

go - 使用公共(public) channel 聚合来自多个 goroutine 的结果

csv - 从 go-kit golang 中的 API 请求解析 CSV 文件

database - ArangoDB - 备份和恢复所有数据

ArangoDB/AQL 更新嵌套文档

typescript - 在 Angular2 Web 应用程序中使用 ArangoJS 驱动程序

arangodb - 如何避免与ArangoDB中的AQL冲突?

go - Reflect showing different a different type than 错误

go - 从 Tcl 脚本调用 golang 函数

postgresql - 需要在事务中插入的当前行的 ID