如果存在,则替换rethinkdb中的数组元素,否则插入

时间:2016-07-25 07:51:19

标签: javascript python arrays rethinkdb rethinkdb-python

{
  "LOGIN": "ABC",
  "MESSAGE_UNPROCESSED": [
    {
      "DATE": "20160219",
      "MESSAGE": [
        {
          "address": "XYZ",
          "date": "1468385398746"
        },
        {
          "address": "PQR",
          "date": "1468385398746"
        }
      ]
    },
    {
      "DATE": "20160220",
      "MESSAGE": [
        {
          "address": "LMN",
          "date": "1468385398746"
        },
        {
          "address": "JXT",
          "date": "1468385398746"
        }
      ]
    }
  ],
  "id": "e1705cae-2a57-42b3-af2e-8458ae9a43bb"
}

如果ABC不存在或者如果存在,我想在登录为DATE的字段中添加消息我想从doc替换整个对象

我试过了:

r.db('usersData').table('smsRaw').filter(function (row) {
    return row('MESSAGE_UNPROCESSED')('DATE').contains('20160222').replace({"DUMP":"DUMP"})
})

error: Cannot nest writes or meta ops in stream operations.  Use FOR_EACH instead in:
r.db("usersData").table("smsRaw").filter(function(var_13) {
    return var_13("MESSAGE_UNPROCESSED")("DATE").contains("20160222").replace({"DUMP": "DUMP"}); 
})

所以如果记录{"DATE":"2016018","MESSAGE":[{"address":"abc","Date":"121212"}]}  如果此记录在MESSAGE_UNPROCESSED中不存在,则应使用set_insert添加,否则如果日期存在,如2016020,则应替换数组中的整个元素

r.db('usersData').table('smsRaw').get("7cd66671-bb01-483d-a148-12ed1b7c2d31").update(function(row) {
    return {
        MESSAGE_UNPROCESSED: r.branch(
            row("MESSAGE_UNPROCESSED")('DATE').eq('20160222'),
            row("MESSAGE_UNPROCESSED")('MESSAGE').merge({"DUMP":"DUMP"}),
            row("MESSAGE_UNPROCESSED")('MESSAGE').replace({"NEW":"NEW"})
        )
    }
})         

无法嵌套写入

我想使用python

进行此操作

0 个答案:

没有答案