go-gorm / go-gorm/datatypes

query for JSON attributes and other query parameters

Open
#287 0 comments 0 reactions 1 assignee View on GitHub

@jinzhu is already working on this.

Since Mar 10, 2025.

Dominant language
Go
Stars
831
Forks
124
PR merge metrics
No merged PRs in 30d

Description

## Your Question

It's unclear how to:
- search for JSON fields in addition to other parameters in a query
- search for multiple JSON fields simultaneously

Given this:
```
type UserWithJSON struct {
gorm.Model
Name string
Attributes datatypes.JSON
}

DB.Create(&UserWithJSON{
Name: "json-1",
Attributes: datatypes.JSON([]byte(`{"name": "jinzhu", "age": 18, "tags": ["tag1", "tag2"], "orgs": {"orga": "orga"}}`)),
}
```

I should be able to query by Name *AND* multiple JSON fields, something along those lines:
```
res := dc.DB.WithContext(ctx).Where([]Row{{
Name: "jinzhu",
Attributes: *datatypes.JSONQuery("item_info").Equals(map[string]interface{"name":"jinzhu", "tags": []string{"tag1"}}),
}}).Find(&rows)
```
If it's possible, it's not clear to me how to. Thanks.

## The document you expected this should be explained

README.

## Expected answer

Clear example on how to filter by multiple cumulative fields.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.