小编Jac*_*ner的帖子

RethinkDB - 更新嵌套数组

我有一个调查表,看起来像这样:

{
  id: Id,
  date: Date,
  clients: [{
    client_id: Id,
    contacts: [{
      contact_id: Id,
      score: Number,
      feedback: String,
      email: String
    }]
  }]
}
Run Code Online (Sandbox Code Playgroud)

我需要更新特定联系人下的字段scorefeedback字段.目前,我正在运行这样的更新:

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;
}; …
Run Code Online (Sandbox Code Playgroud)

javascript database node.js rethinkdb

16
推荐指数
2
解决办法
4468
查看次数

标签 统计

database ×1

javascript ×1

node.js ×1

rethinkdb ×1