Mnesia read/2 with a list of keys instead of just one?

Hello everybody, Iam triying to use mnesia:read/2 with a list of keys instead of just one, for example :

mnesia:read(Tab, [X1, X2,....])

is there any way to do that without using QLC ?

2 Likes

Well, you can just use a normal list comprehension, like

[E || K <- Keys, L <- mnesia:read(table, K), E <- L]
4 Likes

Yes exactly, today I have found this solution it’s just simple by doing a list generator

[mnesia:read(Tab, X) || X <- [X1, X2,....]].
2 Likes

Just be aware that read always returns a list, even if the table has a unique key, that’s why I included the additional loop.

2 Likes

Ah okey Thank you for that

2 Likes

@filmor just the last thing, can we use the same expression with mnesia:select/2 ? for example to extract all FirstNames associated with a given list of LastNames from a table :

MatchHead = #person{firstname='$1', lastName ='$2', _='_' },

Guard = {$2 <- MyList}, 
Result = '$1',
mnesia:select(Tab,[{MatchHead, [Guard], [Result]}]).

the Guard is not correct, I did it just to clear what I want to do, so how we can do that ?

1 Like