Search code examples
databaseerlangmnesia

How to write "in" query for Erlang mnesia?


I have a mnesia table, lets say employee. I need to find all employee records whose name is in EmployeeNameList = ["Erlich", "Richard", "Gilfoyle", "Dinesh"]. Is there a way to do this using mnesia:select or other function?


Solution

  • Following the documentation of Mnesia It can be done as follows:

    get_employees_by_name(NameList) -> 
      MatchHead = #employee{name = '$1', _ = '_'},
      Result = '$_'
      MatchSpec = [ { MatchHead, [{'=:=', '$1', Name}], [Result]} || Name <- NameList ],
      F = fun() -> 
         mnesia:select(employee, MatchSpec)
      end,
      {atomic, Result} = mnesia:transaction(F),
      Result.