Gabriel M
Gabriel M

Reputation: 3

rethinkdb update nested array

I have the following structure:

{
    "id": "3065e957-56e9-4084-8e32-bb4de8d9265a",
    "id_service": "570b4abe-70fe-44e0-845e-74eb60081fc4",
    "tweets": [
        {
            "created_at": "2013-10-13 00:58:11",
            "id_tweet": "389193311908413440",
            "id_user": 12375562,
            "name": "elgabo1",
            "photo": "https://pbs.twimg.com/profile_images/1827710728/45d1be6d2e0f1c710814e098d6f56c12_normal.png",
            "screen_name": "elgabo1",
            "status_tweet": 0,
            "text": "@profeco Deurope Gran Sur tapa los sellos de suspensión con propaganda"
        },
        {
            "created_at": "2013-10-02 06:50:01",
            "id_tweet": "385295588377387008",
            "id_user": 12375562,
            "name": "elgabo1",
            "photo": "https://pbs.twimg.com/profile_images/1827710728/45d1be6d2e0f1c710814e098d6f56c12_normal.png",
            "screen_name": "elgabo1",
            "status_tweet": 1,
            "text": "@caspoliciadf Delegacion coyoacan"
        }
    ]
}

I want to set the status_tweet:1 from id_tweet:"389193311908413440"

I can get the element of the array with the id_tweet:"389193311908413440"

r.db('twitter_settings').table('tweets_service').filter({id:'3065e957-56e9-4084-8e32-bb4de8d9265a'})
    .map(function(d){
        return {
            "tweets": d("tweets").filter({'id_tweet':'665305939294056448'})
        }
    }
    )

But when i try to update the field I get an error:

r.db('twitter_settings').table('tweets_service').filter({id:'3065e957-56e9-4084-8e32-bb4de8d9265a'})
    .map(function(d){
        return {
            "tweets": d("tweets").filter({'id_tweet':'665305939294056448'})
        }
    }
    )
    .update({"status_tweet": 1})


e: Expected type SELECTION but found SEQUENCE:
VALUE SEQUENCE in:
r.db("twitter_settings").table("tweets_service").filter({id: "3065e957-56e9-4084-8e32-bb4de8d9265a"}).map(function(var_341) { return {tweets: var_341("tweets").filter({id_tweet: "665305939294056448"})}; }).update({status_tweet: 1})
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 

Thanks in advance

Upvotes: 0

Views: 215

Answers (1)

kureikain
kureikain

Reputation: 2314

We cannot update/delete after using map.

So change your function to run map inside the update function. Something like this will work.

r.db('twitter_settings').table('tweets_service')
  .filter({id:'3065e957-56e9-4084-8e32-bb4de8d9265a'})

  .update(function(doc) {
    return {
      tweets: doc('tweets').map(function(tweet) {
        return r.branch(
          tweet('id_tweet').eq('389193311908413440'),
          tweet.merge({status_tweet:1}),
          tweet)
      })
    }
  })

Upvotes: 0

Related Questions