【问题标题】:RethinkDB: Get only one record from cursor/selectionRethinkDB:​​从游标/选择中仅获取一条记录
【发布时间】:2023-03-24 22:52:02
【问题描述】:

如果我有一个返回多个结果的查询,我如何从选择中获取单个元素?

例如

r.db("test").table("things") // returns an array of things. I want one of them

使用limit(1) 不是我想要的,因为它会返回一个数组。

【问题讨论】:

    标签: rethinkdb reql


    【解决方案1】:

    Rethink DB 支持获取nth element,因此查询应该是:

    r.db("test").table("things").nth(0)
    

    如果没有元素,上述操作将失败:

    索引超出范围:0

    解决方案是在不存在元素的情况下返回一个默认对象(在我的情况下为 null)。

    r.db("test").table("things").nth(0).default(null)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-23
      相关资源
      最近更新 更多