Skip to content

Limit accept a function and work like reduce #3724

Open

Description

@wojons

Hey guys,

Ran across a problem that would be cool if it can be directly solved in rethinkdb.

Example: If i was building an app that booking air planes. The plane as so many seats and can handle so much weight. I need to make sure the passanger manifest does not go over eater limit.

r.table('manifest').getAll('ABC', {index: 'fligt_number'}).filter(r.row('kg').lt(1000)).limit(r.agg().lte(1000000), function(agg, doc) {
return agg.add(doc('kg'))
}).limit(525)

This would first look in the manifest table and get everyone on the flight abc. and it would filter out anyone that was over 1000kg, then we would have a limit operation. this has 2 parts the check function which is to check to see if the result of the function that falls before is less then or eque to 100000 if its gets over then we will stop accepting docs at the limit and move on to the last limit to sure no more then 525 people get onto the flight. now the second arg takes a function that has 2 args the result from the previus run of the function and the document we are passing on. its simply going to keep adding docs until we get to the limit check we put before.

Activity

  1. added this to the subsequent milestone on Feb 4, 2015
  2. AtnNn commented on Feb 5, 2015

    @AtnNn
    Member

    @wojons I sketched out a proposal for a new command called scan in #3736. @danielmewes pointed out that it is similar to what you propsed here. I'm not sure I fully understand what you are trying to do or what the semantics of r.agg would be, but I think your query could be expressed like this using scan:

    r.table('manifest')
     .getAll('ABC', {index: 'flight_number'})
     .filter(r.row('kg').lt(1000))
     .scan(0, function(total, doc) {
       return total.add(doc('kg')).do(function(new_total){
         return r.branch(new_total.lte(1000000), [new_total, [doc]], null)})})
     .limit(525)
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions