更新RethinkDB中子子数组中的单个值

时间:2016-01-14 13:27:04

标签: rethinkdb

我们正在尝试更新子子数组中的单个答案。 但是,我们的查询导致以下错误:

{

    "deleted": 0 ,
    "errors": 1 ,
    "first_error": "Inserted value must be an OBJECT (got ARRAY):
    [
        {
            "answers":  [
                {
                    "answer":   "wassup",
                    "owner":    12201836
                }
            ],
            "question": "Vraag 1?",
            "questionId":   0,
            "time": "10"
        },
        {
            "answers":  [],
            "question": "Vraag 2?",
            "questionId":   1,
            "time": "15"
        },
        {
            "answers":  [],
            "question": "Vraga 3?",
            "questionId":   2,
            "time": "20"
        }
    ]" ,
    "inserted": 0 ,
    "replaced": 0 ,
    "skipped": 0 ,
    "unchanged": 0

}

我们的表结构如下所示:

Youtube

 - Id
 - Course
 - Unit
 - Session
 - Number
 - Group
 - Questions (array)
      - Question Id
      - Time
      - Answers (array)
            - Id
            - Answer
            - Owner

我们的查询:

   r.db('GitSmurf')
  .table('youtube')
    .update(function (row) { 
      return row('questions').merge(function (q) { 
        return r.branch(q('questionId').eq(0), { "answers": q('answers').merge(function(answer) { 
          return r.branch(answer('owner').eq(12201836), {"answer": "wassup"}, {})} )},{}) 
      })
    })

测试内容:

{
    "completed": [ ],
    "course": "swd" ,
    "group": "dwa-group-b" ,
    "id": "44443377-ed15-4358-a005-f561e7b6a42d" ,
    "number": 1 ,
    "session": 1 ,
    "unit": 1,
    "questions": [
        {
            "answers": [
                {
                    "answer": "hallo" ,
                    "owner": 12201836
                }
            ] ,
            "question": "Vraag 1?" ,
            "questionId": 0 ,
            "time": "10"
        } ,
        {
            "answers": [ ],
            "question": "Vraag 2?" ,
            "questionId": 1 ,
            "time": "15"
        } ,
        {
            "answers": [ ],
            "question": "Vraga 3?" ,
            "questionId": 2 ,
            "time": "20"
        }
    ] ,
}

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

我们忘记在更新查询中返回一个新对象。 当我们补充说它有效。

r.db('GitSmurf')
  .table('youtube')
    .update(function (row) { 
      return { questions: row('questions').merge(function (q) { 
        return r.branch(q('questionId'), { "answers": q('answers').merge(function(answer) { 
          return r.branch(answer('owner').eq(12201836), {"answer": "tom"}, {})
        })},{}) 
      })}
    })