Exadra37

Exadra37

Mnesia Bag table - How to wrtie a query to return results grouped by bag

I am using Mnesia via Memento with the table type bag:

defmodule Tasks.Todos.Schema.V2.Todo do

  alias Tasks.Todos.Schema.V2.Todo

  use Memento.Table,
    attributes: [
      :uid,
      :user_uid,
      :date,
      :title,
      :done,
      :hash,
      :inserted_at,
      :updated_at,
    ],
    index: [
      :user_uid,
      :hash,
      :date,
      # :done,
    ],
    # https://learnyousomeerlang.com/ets#the-concepts-of-ets
    type: :bag
end

The intention of using the type bag is to keep all versions of each updated Todo.

A set of records in the database looks like this:

iex(28)> Tasks.Todos.list_by_date! "2020-06-26", "5de054556a456423c7d7a2778e8fb53fef43ce8d721db835575e76b436e6a804"
[
  %Tasks.Todos.Schema.V2.Todo{
    __meta__: Memento.Table,
    date: "2020-06-26",
    done: false,
    hash: "b1fed2ef2aea274923fb69a83dbc76fe670272c67e4f7cd68651aebfdc34a2ad",
    inserted_at: ~N[2020-06-26 07:55:52.362966],
    title: "Phoenix",
    uid: "24d06f5ece1025fa560e50f85ea23fe5ecb651e5506c4e28f1b216816a53ba0d",
    updated_at: ~N[2020-06-26 07:55:52.362966],
    user_uid: "5de054556a456423c7d7a2778e8fb53fef43ce8d721db835575e76b436e6a804"
  },
  %Tasks.Todos.Schema.V2.Todo{
    __meta__: Memento.Table,
    date: "2020-06-26",
    done: false,
    hash: "3b92c6310c3223215dada16e04cc024cfb7cb9d98626247c5d1eae69d12ed808",
    inserted_at: ~N[2020-06-26 07:56:50.480292],
    title: "Upgrade to Elixir 10",
    uid: "0a4e526765b6a4897ea83437f291009f6698035156f740e3e1b6c44118236fab",
    updated_at: ~N[2020-06-26 07:56:50.480292],
    user_uid: "5de054556a456423c7d7a2778e8fb53fef43ce8d721db835575e76b436e6a804"
  },
  %Tasks.Todos.Schema.V2.Todo{
    __meta__: Memento.Table,
    date: "2020-06-26",
    done: false,
    hash: "c8db6213e7e3cb853cf729052ee69eff9a7f95eb09413e60f0ab7146781dde57",
    inserted_at: ~N[2020-06-26 07:56:50.480292],
    title: "Upgrade to Elixir 10.3",
    uid: "0a4e526765b6a4897ea83437f291009f6698035156f740e3e1b6c44118236fab",
    updated_at: ~N[2020-06-26 07:56:58.465946],
    user_uid: "5de054556a456423c7d7a2778e8fb53fef43ce8d721db835575e76b436e6a804"
  },
  %Tasks.Todos.Schema.V2.Todo{
    __meta__: Memento.Table,
    date: "2020-06-26",
    done: false,
    hash: "7a39cc364031ad778d322ff0fe49feb2854534f1e07de1da52e4f2706bd284b5",
    inserted_at: ~N[2020-06-26 07:55:08.163917],
    title: "Elixir",
    uid: "05ce55338ac4f782c3da584944f23f02868442831d5d0d3432b86bb23616ed7c",
    updated_at: ~N[2020-06-26 07:55:08.163917],
    user_uid: "5de054556a456423c7d7a2778e8fb53fef43ce8d721db835575e76b436e6a804"
  }
]

When queering by date I get all todos as expected, but I would like the result to be grouped by bag.

So from the records above I would like to have the Todo with title "Upgrade to Elixir 10" and "Upgrade to Elixir 10.3" grouped together… Yes I know that is like asking for group by from SQL, but after all this a bag table, thus I think it is reasonable to have this expectation :wink:

My query:

Memento.transaction fn -> Memento.Query.select(Todo, [{:==, :user_uid, user_uid}, {:==, :date, date}]) end

I tried to read the Erlang docs about Mnesia but I was not able to figure out any way of accomplish what I want, maybe is because I don’t know Erlang or because its not really possible to do with the query syntax.

NOTE: I know that I can sort this out with ELixir when I get the records back, but that is not what I am asking for :wink:

First Post!

JeyHey

JeyHey

I don’t know the solution but a workaround would be to do it in 2 steps:

  1. Get all keys with mnesia:all_keys/1 (or mnesia:dirty_all_keys/1)
  2. Read all keys with mnesia:read/3. This gives a list with the bag for every key.

This can be done in a transaction to lock the db I think.

Where Next?

Popular in Questions Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
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
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: <h1>Create Post</h1> <%= ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
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
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29603 241
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
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
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement