如何在 Tarantool 中 select 数据?
How to select data in Tarantool?
我创建了一个 space 有两个索引:
box.schema.sequence.create('user_seq', { if_not_exists = true })
box.schema.create_space('users', { if_not_exists = true, format={
{ name = 'id', type = 'unsigned'},
{ name = 'name', type = 'string'},
{ name = 'age', type = 'unsigned'}}
})
box.space.users:create_index('pk', { parts = { 'id' }, if_not_exists = true })
box.space.users:create_index('age', { parts = { 'age' }, if_not_exists = true })
已插入一些记录:
box.space.users:insert({ box.sequence.user_seq:next(), 'Artur Barsegyan', 24})
box.space.users:insert({ box.sequence.user_seq:next(), 'Kostya Nazarov', 32})
我应该如何select数据?
如果您需要通过索引进行迭代,您应该在所需的索引上调用 pairs
方法。例如:
for _, tuple in box.space.users.index.age:pairs({30}, { iterator = 'LT' }) do
print(tuple)
end
您将得到以下结果:
[1, 'Artur Barsegyan', 24]
---
...
我们做了什么?我们已经通过索引 age
迭代了所有 age
小于 30 的元组。关于迭代器和其他参数和过滤器的更多细节是 in the documentation.
如果您想 select space 中的所有元组,请在 space 对象上调用 pairs
方法而不带参数:
tarantool> for i, tuple in box.space.users:pairs() do
> print(tuple)
> end
[1, 'Artur', 'Barsegyan', 24, {'gender': 'male', 'position': 'Product Manager'}]
[2, 'Kostya', 'Nazarov', 32, {'gender': 'male', 'position': 'Lead of Tarantool Solutions'}]
---
...
探索更多参数和过滤器 in documentation 并在以下 SO 问题中:
- How do I select a limited number of records from tarantool, like with SELECT LIMIT in SQL?
为什么不以简单的方式做事?
box.schema.space.create "users"
box.space.users:format { { name="id", type="unsigned"} --[[and so on]] }
box.space.users:create_index("primary", { sequence = true })
box.space.users:insert { nil, "John Doe", 20 }
box.space.users:insert { nil, "Jon Snow", 20 }
local all_users = box.space.users:select()
我创建了一个 space 有两个索引:
box.schema.sequence.create('user_seq', { if_not_exists = true })
box.schema.create_space('users', { if_not_exists = true, format={
{ name = 'id', type = 'unsigned'},
{ name = 'name', type = 'string'},
{ name = 'age', type = 'unsigned'}}
})
box.space.users:create_index('pk', { parts = { 'id' }, if_not_exists = true })
box.space.users:create_index('age', { parts = { 'age' }, if_not_exists = true })
已插入一些记录:
box.space.users:insert({ box.sequence.user_seq:next(), 'Artur Barsegyan', 24})
box.space.users:insert({ box.sequence.user_seq:next(), 'Kostya Nazarov', 32})
我应该如何select数据?
如果您需要通过索引进行迭代,您应该在所需的索引上调用 pairs
方法。例如:
for _, tuple in box.space.users.index.age:pairs({30}, { iterator = 'LT' }) do
print(tuple)
end
您将得到以下结果:
[1, 'Artur Barsegyan', 24]
---
...
我们做了什么?我们已经通过索引 age
迭代了所有 age
小于 30 的元组。关于迭代器和其他参数和过滤器的更多细节是 in the documentation.
如果您想 select space 中的所有元组,请在 space 对象上调用 pairs
方法而不带参数:
tarantool> for i, tuple in box.space.users:pairs() do
> print(tuple)
> end
[1, 'Artur', 'Barsegyan', 24, {'gender': 'male', 'position': 'Product Manager'}]
[2, 'Kostya', 'Nazarov', 32, {'gender': 'male', 'position': 'Lead of Tarantool Solutions'}]
---
...
探索更多参数和过滤器 in documentation 并在以下 SO 问题中:
- How do I select a limited number of records from tarantool, like with SELECT LIMIT in SQL?
为什么不以简单的方式做事?
box.schema.space.create "users"
box.space.users:format { { name="id", type="unsigned"} --[[and so on]] }
box.space.users:create_index("primary", { sequence = true })
box.space.users:insert { nil, "John Doe", 20 }
box.space.users:insert { nil, "Jon Snow", 20 }
local all_users = box.space.users:select()