How to extract multiple queries at once in Rethink

2019-09-05 00:03发布

问题:

I want to do something like:

var tab = r.db("test").table("test"); all =[ tab.getAll('1').fitler({'hidden': false}).limit(1), tab.getAll('2').fitler('hidden': false}).limit(1), tab.getAll('3').fitler('hidden': false}).limit(1), ]

But when running this query I'm getting:

Expected type DATUM but found SELECTION:

回答1:

In general, the "Expected type DATUM but found SELECTION" error can be solved by adding .coerceTo('array'):

var tab = r.db("test").table("test");
all =[
  tab.getAll('1').filter({'hidden': false}).limit(1).coerceTo('array'),
  tab.getAll('2').filter({'hidden': false}).limit(1).coerceTo('array'),
  tab.getAll('3').filter({'hidden': false}).limit(1).coerceTo('array')
]

But in this specific case, you can replace .limit(1) with .nth(0):

var tab = r.db("test").table("test");
all =[
  tab.getAll('1').filter({'hidden': false}).nth(0),
  tab.getAll('2').filter({'hidden': false}).nth(0),
  tab.getAll('3').filter({'hidden': false}).nth(0)
]


标签: rethinkdb