2015-05-15 47 views
15

Güncellenmesi Öyle benzeyen bir anket tablo var. Şu anda, böyle güncelleştirmeyi çalıştırıyorum: değer çiftleri:RethinkDB iç içe dizi

function saveScore(obj){ 
    var dfd = q.defer(); 
    var survey = surveys.get(obj.survey_id); 

    survey 
    .pluck({ clients: 'contacts' }) 
    .run() 
    .then(results => { 

     results.clients.forEach((item, outerIndex) => { 
     item.contacts.forEach((item, index, array) => { 
      if(Number(item.contact_id) === Number(obj.contact_id)) { 
      array[index].score = obj.score; 
      console.log(outerIndex, index); 
      } 
     }); 
     }); 

     return survey.update(results).run() 
    }) 
    .then(results => dfd.resolve(results)) 
    .catch(err => dfd.resolve(err)); 

    return dfd.promise; 
}; 

Ben update yönteme baktığımızda, bu iç içe anahtar nasıl güncelleneceğini belirtir. Bununla birlikte, bir dizideki tek bir öğeyi güncelleştirmek için herhangi bir örnek bulamıyorum.

İç içe geçmiş bir dizideki öğeleri güncellemek için daha iyi ve umarız daha temiz bir yol var mı?

cevap

10

Dizide, filter dizisini dizide almanız ve diziye tekrar eklemeniz gerekebilir. Sonra güncelleştirilmiş diziyi update yöntemine iletebilirsiniz.

Örnek

Diyelim ki hem name ve score sahip iki müşterilerle bir belge var diyelim ve bunlardan birinde puan güncellemek istiyorum:

{ 
    "clients": [ 
    { 
     "name": "jacob" , 
     "score": 200 
    } , 
    { 
     "name": "jorge" , 
     "score": 57 
    } 
    ] , 
    "id": "70589f08-284c-495a-b089-005812ec589f" 
} 

Sen alabilirsiniz Bu belirli bir belge, update komutunu bir anonim işlevle çalıştırın ve sonra yeni, güncelleştirilmiş dizide clients özelliğine iletin.

r.table('jacob').get("70589f08-284c-495a-b089-005812ec589f") 
    .update(function (row) { 
    return { 
     // Get all the clients, expect the one we want to update 
     clients: row('clients').filter(function (client) { 
     return client('name').ne('jorge') 
     }) 
     // Append a new client, with the update information 
     .append({ name: 'jorge', score: 57 }) 
    }; 
    }); 

Bu durumun biraz hantal ve bunu yapmanın daha güzel, daha şık bir yolu muhtemelen olduğunu düşünüyorum, ama bu sorunu çözmek gerekir.

Veritabanı Şeması

Belki tüm kişileriniz için contacts tablo oluşturmak ve daha sonra bir çeşit size veriler üzerinde katılmak yapmak değer. gibi Sonra clients dizideki sizin contacts mülkiyet görünecektir:

{ 
    id: Id, 
    date: Date, 
    clients: [{ 
    client_id: Id, 
    contact_scores: { 
     Id: score(Number) 
    }, 
    contact_feedbacks: { 
     Id: feedback(String) 
    } 
    }] 
} 
3

benim için çalışıyor

r.table(...).get(...).update({ 
contacts: r.row('Contacts').changeAt(0, 
    r.row('Contacts').nth(0).merge({feedback: "NICE"})) 
}) 
0

veritabanı şeması sonra

{ 
    "clients": [ 
    { 
     "name": "jacob" , 
     "score": 200 
    } , 
    { 
     "name": "jorge" , 
     "score": 57 
    } 
    ] , 
    "id": "70589f08-284c-495a-b089-005812ec589f" 
} 

bu map kullanmak gibi yapabilirsiniz ve branch sorgu.

r.db('users').table('participants').get('70589f08-284c-495a-b089-005812ec589f') 
    .update({"clients": r.row('clients').map(function(elem){ 
    return r.branch(
     elem('name').eq("jacob"), 
     elem.merge({ "score": 100 }), 
     elem)}) 
    })