Skip to content

More complex documents can not be queried properly #143

@rababarber

Description

@rababarber

I'm experiencing strange behaviour when trying to query more complex JSON documents in embedded mode. When the document that is inserted inserted to tiedot is created from a string via json.Unmarshal() it can be queried successfully. In case it is created from a directly initialized object it can not be queried.

Example object is taken from the example in documentation:

{"Name": [
  {"PenName": "Joshua"}, 
  {"PenName": ["John", "David"]}
]}

Consider the following code snippet:

_, err = coll.Insert(jsonDoc)

query := map[string]interface{}{
	"eq": "John",
	"in": []interface{}{"Name", "PenName"},
}

queryResult := make(map[int]struct{})
if err := db.EvalQuery(query, coll, &queryResult); err != nil {
	panic(err)
}

Query returns any data only in a case when jsonDoc is created as:

s := `{"Name": [{"PenName": "Joshua"}, {"PenName": ["John", "David"]}]}`
var jsonDoc map[string]interface{}
json.Unmarshal([]byte(s), &jsonDoc)

but does not work when created as:

jsonDoc := map[string]interface{}{
  "Name": []interface{}{
    map[string]interface{}{
      "PenName": "Joshua",
    },
    map[string]interface{}{
      "PenName": []string{"John", "David"},
    },
  },
}

From my understanding these two methods of creating jsonDoc are equivalent. What I'm missing?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions