ETS表选择所有记录匹配ID
嘿,我有一个ETS表交易
,表的一部分看起来像这样。
[
{"txn_05h0f14877a091yj7358a",
%{
account_id: "acc_f5f3f6y7t8s7a5htj78ty",
amount: 289,
date: ~D[2022-03-16],
details: %{
details: %{
category: "transport",
counterparty: %{name: "UBER", type: "organization"},
processing_status: "complete"
}
},
links: %{
account: "http://localhost:4000/accounts/acc_f5f3f6y7t8s7a5htj78ty",
self: "http://localhost:4000/accounts/acc_f5f3f6y7t8s7a5htj78ty/balances/transactions/txn_f5f3f6y7t8s7a5htj78ty"
},
running_balance: 100000,
status: "posted",
type: "card_payment"
}}
]
我想选择与account_id
匹配的所有记录,我正在做这样的记录
iex(23)> fun = :ets.fun2ms(fn {_, {account_id}} when account_id == "acc_f5f3f6y7t8s7a5htj78ty" -> account_id end)
[{{:_, {:"$1"}}, [{:==, :"$1", "acc_f5f3f6y7t8s7a5htj78ty"}], [:"$1"]}]
iex(24)> :ets.select(:transactions, fun)
[]
,但是查询无法正常工作。选择匹配account_id
的所有记录的正确方法是什么?
Hey I have an ets table transactions
, part of the table is looking like this.
[
{"txn_05h0f14877a091yj7358a",
%{
account_id: "acc_f5f3f6y7t8s7a5htj78ty",
amount: 289,
date: ~D[2022-03-16],
details: %{
details: %{
category: "transport",
counterparty: %{name: "UBER", type: "organization"},
processing_status: "complete"
}
},
links: %{
account: "http://localhost:4000/accounts/acc_f5f3f6y7t8s7a5htj78ty",
self: "http://localhost:4000/accounts/acc_f5f3f6y7t8s7a5htj78ty/balances/transactions/txn_f5f3f6y7t8s7a5htj78ty"
},
running_balance: 100000,
status: "posted",
type: "card_payment"
}}
]
I would like to select all records that are matching account_id
and I'm doing something like this
iex(23)> fun = :ets.fun2ms(fn {_, {account_id}} when account_id == "acc_f5f3f6y7t8s7a5htj78ty" -> account_id end)
[{{:_, {:"$1"}}, [{:==, :"$1", "acc_f5f3f6y7t8s7a5htj78ty"}], [:"$1"]}]
iex(24)> :ets.select(:transactions, fun)
[]
but the query is not working properly. What will be a proper way of selecting all records matching account_id
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的方法有效,但您的模式与数据的形状不符:它期望形状
{“ TXN_05H0F14877A091YJ7358A”,{“ ACC_F5F3F6Y7T8S7A5HTJ78TY}}}
。您需要查找地图
:account_id
键:注意:
transaction.account_id ==
guard需要elixir> = 1.11,您还可以使用何时: erlang.map_get(:account_id,transaction)==
如果您在较旧版本上。Your approach works but your pattern doesn't match the shape of the data: it expects records of the shape
{"txn_05h0f14877a091yj7358a", {"acc_f5f3f6y7t8s7a5htj78ty"}}
.You need to lookup the map
:account_id
key:Note: The
when transaction.account_id ==
guard needs Elixir >= 1.11, you can also usewhen :erlang.map_get(:account_id, transaction) ==
if you are on an older version.