rethinkdb更新嵌套数组

时间:2015-11-21 00:45:40

标签: rethinkdb rethinkdb-javascript

我有以下结构:

{
    "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"
        }
    ]
}

我想从id_tweet设置status_tweet:1:" 389193311908413440"

我可以使用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'})
        }
    }
    )

但是当我尝试更新字段时,我收到一个错误:

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})
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 

提前致谢

1 个答案:

答案 0 :(得分:0)

使用map后,我们无法更新/删除。

因此,更改您的函数以在更新函数内运行map。这样的事情会起作用。

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)
      })
    }
  })