RethinkDB - Updating nested array

后端 未结 6 2318
隐瞒了意图╮
隐瞒了意图╮ 2021-02-13 02:09

I have a survey table that looks like so:

{
  id: Id,
  date: Date,
  clients: [{
    client_id: Id,
    contacts: [{
      contact_id: Id,
      score: Number,
         


        
6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-13 02:21

    Better late than never

    I had your same problem and i could solve it with two ways:

    With specific client_id

    r.db('nameDB').table('nameTable').get('idRegister')
    .update({'clients': r.row('clients')
        .map(elem=>{
            return r.branch(
                elem('client_id').eq('your_specific_client_id'),
                elem.merge({
                    contacts: elem('contacts').map(elem2=>
                        r.branch(
                            elem2('contact_id').eq('idContact'),
                            elem2.merge({
                                score: 99999,
                                feedback: 'yourString'
                            }),
                            elem2
                        )
                    )
                }),
                elem
            )
        })
    })
    

    Without specific client_id

    r.db('nameDB').table('nameTable').get('idRegister')
    .update({'clients': r.row('clients')
        .map(elem=>
            elem.merge({
                contacts: elem('contacts').map(elem2=>
                    r.branch(
                        elem2('contact_id').eq('idContact'),
                        elem2.merge({
                            score: 99999,
                            feedback: 'yourString'
                        }),
                        elem2
                    )
                )
            })
        )
    })
    

    I hope that it works for you, even when happened much time ago

提交回复
热议问题