按可选字段重新思考数据库顺序

时间:2015-07-16 08:36:50

标签: rethinkdb

文档:

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

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

我该怎么做?

1 个答案:

答案 0 :(得分:3)

You can use function as mentioned heredefault

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