vasspilka

vasspilka

Ecto nested embeds with jsonb

Hello, I am having some trouble with nested embeds, I have a jsonb column metadata which looks like this:

  field(:collected_from, :string)
  field(:era, :string)

  embeds_one(:file, File)
  embeds_one(:sync, Sync)
end

But when try to get something from file I cannot, it looks like the File embed is saved as string.
Repo.all(from d in "data", select: fragment("metadata->>'file'")) This returns something like:

"{\"id\": \"63769d9b-fee8-456d-afae-96e8a9efaaa7\", \"line\": 4, \"path\": \"/home/x/Work/EPark/eparkomat/_build/dev/lib/eparkomat/priv/mock_folder/labels/6219321040015.MOV.txt\", \"file_created_time\": \"2018-03-26T12:33:18\", \"file_accessed_time\": \"2018-05-07T10:26:58\", \"file_modified_time\": \"2018-03-26T12:33:18\"}",
"{\"id\": \"9420931f-afa6-42d7-b5b7-7a4bd42a670e\", \"line\": 9, \"path\": \"/home/x/Work/EPark/eparkomat/_build/dev/lib/eparkomat/priv/mock_folder/labels/6219321040015.MOV.txt\", \"file_created_time\": \"2018-03-26T12:33:18\", \"file_accessed_time\": \"2018-05-07T10:26:58\", \"file_modified_time\": \"2018-03-26T12:33:18\"}",
"{\"id\": \"2a362e2f-9eb4-4462-bff6-42f21e8e47e4\", \"line\": 3, \"path\": \"/home/x/Work/EPark/eparkomat/_build/dev/lib/eparkomat/priv/mock_folder/labels/8689819590011.MOV.txt\", \"file_created_time\": \"2018-05-07T10:25:19\", \"file_accessed_time\": \"2018-05-07T10:25:19\", \"file_modified_time\": \"2018-05-07T10:25:19\"}"]

But when I do Repo.all(from d in "data", select: fragment("metadata->>'file.line'")) it just returns nils

Any ideas?

Marked As Solved

vasspilka

vasspilka

Ok so what I ended up doing is something like the following

fragment("((metadata->>'file')::json->'line')::text::int") seems to work :slight_smile:

Or what @jtranin suggested fragment("(metadata#>>'{file, line}')::integer ASC")

Where Next?

Popular in Questions Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

We're in Beta

About us Mission Statement