Skip to content

ReQL problem: updating arrays #2056

Closed
Closed

Description

@coffeemug

Our interface for updating arrays inside an object is very clunky. Consider a sample document from a table posts with an array of comments:

{
  post_id: ...,
  text: ...,
  comments: [ { comment_id: ..., comment_text: ...}, ... ]
}

Suppose the user wants to find a comment with a particular id (in a post with a particular id), and update the comment's text. I can proceed to type up a query to do it, but it's obviously extremely annoying (too annoying for me to type it up, in fact).

The same problem applies if we want to delete a particular comment, or do any kind of modification to the array. I don't quite know what an elegant way to solve this is. Leaving in subsequent for now.

Activity

  1. added this to the subsequent milestone on Mar 4, 2014
  2. coffeemug commented on Mar 20, 2014

    @coffeemug
    ContributorAuthor

    @neumino suggested a couple of commands in https://github.com/rethinkdb/company/issues/17#issuecomment-37899006:

    Add methods like updateField, deleteWhere, updateWhere (we would have to find better names)

    Add an element in a field

    r.table("posts").updateField("comments", function(comments) {
        return comments.append({author: "Michel", comment: "Hello" })
    })
    

    Delete an element in an array

    r.table("posts").updateField("comments", function(comments) {
        return comments.deleteWhere(function(comment) {
            comment("author").eq("Michel")
        })
    })
    

    Update an element in an array

    r.table("posts").updateField("comments", function(comments) {
        return comments.updateWhere(
            function(comment) { return comment("author").eq("Michel") },
            function(comment) { return comment.updateField("author", "Michel T.") }
         )
    })
    

    The point of updateField is to easily update a nested field without having to do tons of map/do/merge (so the first argument can be {nested: {field: true}}
    This syntax is close to Mongo's one, and we would provide something better in some cases -- e.g: https://jira.mongodb.org/browse/SERVER-1243

    Marking as proposal.

  3. mlucy commented on Mar 20, 2014

    @mlucy
    Member

    Rather than having updateField, we could introduce a function pseudotype and let people write:

    r.table('posts').update({comments: lambda {|comments| ...})

    This would also let people update multiple fields at once.

  4. modified the milestones: , subsequent on Mar 26, 2014
  5. modified the milestones: 1.14, subsequent on Jun 12, 2014
  6. AtnNn commented on Jun 20, 2014

    @AtnNn
    Member

    @mlucy A function pseudo-type seems like a very good idea.

    deleteWhere seems unnecessary, it is equivalent to filter . not.

    updateWhere is a bad name but would be very useful. Perhaps it could be called mapOn. It would also be useful to have an indexed version too, related to #2574. I would call it imapOn.

    I have also seen many users trying to update a specific index inside an array. Perhaps a shortcut syntax such as {2: 'foo'} could replace the third item in the array?

  7. srh commented on Jun 20, 2014

    @srh
    Contributor

    We can have a singleRowSelection turn into a singleFieldSelection when a field value is selected. You can use .delete or .replace on them. .update can work if they're an object.

    Such an API will also make things straightforward, if/when we support performant updates on individual fields of a row that has other very large fields.

  8. coffeemug commented on Jun 24, 2014

    @coffeemug
    ContributorAuthor

    Moving to 1.14-polish. This isn't as important as major features, and I'd like to agree on those first. We can tackle this later.

  9. modified the milestones: , 1.14 on Jun 24, 2014
  10. modified the milestones: , on Aug 13, 2014
  11. coffeemug commented on Aug 13, 2014

    @coffeemug
    ContributorAuthor

    Closing in favor of #2692.

  12. modified the milestones: , duplicate on Aug 13, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions