Rethinkdb 按可选字段排序

Rethink db order by optional field

文档:

[{
  id: 111,
  weight: 1 
},{
  id: 222,
},{
  id: 333,
  weight: -1
}]

我想按 weight 订购文档,如果 weight 不存在,请将其 weight 视为 0

我该怎么做?

您可以使用提到的功能 here with default:

r.table(TABLE_NAME).orderBy(r.asc(function(doc) {
    return doc("weight").default(0)
}));