Hi! I have a question about Tiedot queries. How do you query the content of an array of structures? I have this program:
// main
package main
import (
"encoding/json"
"fmt"
"strconv"
)
type Arr struct {
A string `json:"A"`
B int `json:"B"`
}
type Str struct {
C int `json:"C"`
D []Arr `json:"D"`
}
func main() {
myDB, err := db.OpenDB("C:\\tmp\\MyDatabase")
defer myDB.Close()
if err != nil {
panic(err)
}
myDB.Drop("A")
myDB.Create("A")
col := myDB.Use("A")
arr1 := Arr{A: "A", B: 1}
arr2 := Arr{A: "a", B: 0}
arr := []Arr{arr1, arr2}
str := Str{C: 2, D: arr}
fmt.Println(str)
var q int
q = 1
fmt.Println("Searching for", q)
_, err2 := col.Insert(str)
if err2 != nil {
panic(err2)
}
result := make(map[uint64]struct{})
var query interface{}
x := `{"eq":{"B":` + strconv.Itoa(q) + `},"in": ["C"]}`
//x := `"all"`
e := json.Unmarshal([]byte(x), &query)
if e != nil {
fmt.Println("Json error:", e)
} else {
fmt.Println("query=", query)
}
e2 := db.EvalQueryV2(query, col, &result)
if e2 != nil {
fmt.Println("Query error:", e2)
} else {
fmt.Println("Proceeding...")
fmt.Println("Result=", result)
}
if result == nil {
fmt.Println("Found nothing")
} else {
for id := range result {
fmt.Println("Found:", id)
var z map[string]interface{}
col.Read(id, &z)
fmt.Println(z)
}
}
fmt.Println("Hello World!")
}
How do I query the contents of D?