Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

query for JSON attributes and other query parameters #287

Open
ericat opened this issue Mar 10, 2025 · 0 comments
Open

query for JSON attributes and other query parameters #287

ericat opened this issue Mar 10, 2025 · 0 comments
Assignees

Comments

@ericat
Copy link

ericat commented Mar 10, 2025

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants