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

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
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
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
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
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

Other popular topics Top

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

We're in Beta

About us Mission Statement