datatypes icon indicating copy to clipboard operation
datatypes copied to clipboard

查询tags 里的tag1的值,怎么做呢?

Open xiebingmengg opened this issue 3 years ago • 3 comments

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

查询tags里,是否有tag1,怎么做呢?谢谢!

xiebingmengg avatar Jan 11 '22 02:01 xiebingmengg

the same question here. how to query a tag is in json array

https://www.postgresql.org/docs/current/datatype-json.html

-- Find documents in which the key "tags" contains key or array element "qui"
SELECT jdoc->'guid', jdoc->'name' FROM api WHERE jdoc -> 'tags' ? 'qui';

ping @jinzhu

ghost avatar Apr 21 '22 06:04 ghost

and more

SELECT jdoc->'guid', jdoc->'name' FROM api WHERE jdoc @@ '$.tags[*] == "qui"';

ghost avatar Apr 21 '22 06:04 ghost

or my project using

		result = common.NewDb.
			Raw("select *  from  gs_article where gs_article.tag_ids @> ? LIMIT ? OFFSET ? ", tagId, rpp, offset).
			Find(&blogDataList)

ghost avatar Apr 28 '22 10:04 ghost