Skip to content

Explain the behaviour of forEach #1261

Open

Description

@NotJustAnna

Apparently, forEach merge and sums all objects given to them? This is undocumented on the docs.

image
image

Activity

  1. chipotle commented on Feb 18, 2020

    @chipotle
    Contributor

    What is it you're actually trying to do? forEach is described with:

    Loop over a sequence, evaluating the given write query for each element.

    And the examples you're giving are not write queries.

    (I could have sworn that forEach had a warning somewhere that it was indeed only for write queries, but apparently not. But IIRC, it really means it, and I have a suspicion that what you want is more properly accomplished with map.)

  2. srh commented on Feb 18, 2020

    @srh
    Contributor

    If you run something like

    r.expr([1,2,3]).forEach(function(a) { return r.table('foo').insert({id: a}); })
    

    it will output

    
    {
    
        "deleted": 0 ,
        "errors": 0 ,
        "inserted": 3 ,
        "replaced": 0 ,
        "skipped": 0 ,
        "unchanged": 0
    }
    

    That result was formed by combining together three different write result objects. The .forEach function adds the stats together and concatenates any arrays together.

    It looks like it doesn't validate that the query result is necessarily a write result, it just evaluates the subqueries. Hence

    r.expr([1,2,3]).forEach(function(a) { return {x: a}; }) returning {x: 6}.

    It will only complain if it gets a value it refuses to merge together, like a number.

    I don't know why you're getting "var_541". I don't get that when I run the query. That's definitely a bug, really a complete mystery; what version of RethinkDB are you running it's clear from the screenshot you're running 2.4.

  3. srh commented on Feb 18, 2020

    @srh
    Contributor

    It definitely could be better documented.

  4. srh commented on Feb 18, 2020

    @srh
    Contributor

    Could I say again how freaky that var_541 thing is? I can't reproduce it in 2.4 or before. What browser are you running? Could it be its JavaScript engine?

  5. NotJustAnna commented on Feb 18, 2020

    @NotJustAnna
    MemberAuthor

    Could I say again how freaky that var_541 thing is? I can't reproduce it in 2.4 or before. What browser are you running? Could it be its JavaScript engine?

    It's the RethinkDB 2.4 Data Explorer

  6. NotJustAnna commented on Feb 19, 2020

    @NotJustAnna
    MemberAuthor

    Managed to reproduce it with literal + instead of add

    image

  7. srh commented on Feb 19, 2020

    @srh
    Contributor

    That's because you wrote a + 1 instead of a.add(1). But in the ...

    Okay. In the screenshot, I guess you ran a + 1, got the output, then edited the query to be a.add(1) without rerunning the query.

  8. NotJustAnna commented on Feb 20, 2020

    @NotJustAnna
    MemberAuthor

    Just to get back on track:

    That result was formed by combining together three different write result objects. The .forEach function adds the stats together and concatenates any arrays together.

    It looks like it doesn't validate that the query result is necessarily a write result, it just evaluates the subqueries. Hence

    r.expr([1,2,3]).forEach(function(a) { return {x: a}; }) returning {x: 6}.

    It will only complain if it gets a value it refuses to merge together, like a number.

    This is what's missing from the docs.

  9. NotJustAnna commented on Feb 20, 2020

    @NotJustAnna
    MemberAuthor

    What is it you're actually trying to do? forEach is described with:

    Loop over a sequence, evaluating the given write query for each element.

    And the examples you're giving are not write queries.

    (I could have sworn that forEach had a warning somewhere that it was indeed only for write queries, but apparently not. But IIRC, it really means it, and I have a suspicion that what you want is more properly accomplished with map.)

    I'm not trying to do anything with it, I really tried to discover what the result type was. I have an issue tracking all RethinkDB types (rethinkdb/rethinkdb-java#18), and forEach just returned an object. No format, explaining, nothing. Just object. That's when I stumbled upon this behaviour.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions