jilen
jilen

Reputation: 5763

Rethink db order by optional field

Docs:

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

I want to order the docs by weight if weight not exists treat its weight as 0

How can I do that ?

Upvotes: 1

Views: 68

Answers (1)

You can use function as mentioned here with default:

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

Upvotes: 3

Related Questions