RethinkDB:仅从 cursor/selection 中获取一条记录
RethinkDB: Get only one record from cursor/selection
如果我有一个 returns 多个结果的查询,我如何从选择中获取单个元素?
例如
r.db("test").table("things") // returns an array of things. I want one of them
使用 limit(1)
不是我想要的,因为 returns 是一个数组。
Rethink DB 支持获取 nth element 所以查询应该是:
r.db("test").table("things").nth(0)
如果没有元素,以上将失败:
Index out of bounds: 0
解决方案是 return 如果不存在任何元素,则使用默认对象(在我的例子中为 null)。
r.db("test").table("things").nth(0).default(null)
如果我有一个 returns 多个结果的查询,我如何从选择中获取单个元素?
例如
r.db("test").table("things") // returns an array of things. I want one of them
使用 limit(1)
不是我想要的,因为 returns 是一个数组。
Rethink DB 支持获取 nth element 所以查询应该是:
r.db("test").table("things").nth(0)
如果没有元素,以上将失败:
Index out of bounds: 0
解决方案是 return 如果不存在任何元素,则使用默认对象(在我的例子中为 null)。
r.db("test").table("things").nth(0).default(null)