Search code examples
nestedcouchbasesql++

Nested N1QL Update Query


I searched some prior threads, but can't quite figured out what I'm doing wrong based on how I've implemented my N1QL update query for Couchbase. The query I'm running to select my target updates is working fine:

SELECT p FROM `NoSQLDB` AS c UNNEST c.phones as p
WHERE c.type='com.model' AND p.typeCode != 'B_BUS'

this provides the following results:

[
  {
    "p": {
      "type": "com.model.Phone",
      "typeCode": "H_HOME",
    }
  },
  {
    "p": {
      "type": "com.model.Phone",
      "typeCode": "H_HOME",
    }
  }
]

Which is the data I want to update, however, when I go to update typeCode using the following update statement, it executes but doesn't actually change anything:

UPDATE `NoSQLDB` AS c SET p.typeCode = 'B_BUS'
FOR p WITHIN c.phones WHEN c.type='com.model' AND
p.typeCode != 'B_BUS' END LIMIT 1

I also tried this which did nothing as well, thinking maybe the parent type wouldn't be available:

UPDATE `NoSQLDB` AS c SET p.typeCode = 'B_BUS'
FOR p WITHIN c.phones WHEN p.type='com.model.Phone' AND
p.typeCode != 'B_BUS' END LIMIT 1

The parent JSON looks like the following if it's any relevancy:

[
  {
    "NoSQLDB": {
      "id": "01234",
      "keyType": "BANANA",
      "phones": [
        {
          "type": "com.model.Phone",
          "typeCode": "H_HOME",
        },
        {
          "type": "com.model.Phone",
          "typeCode": "B_BUS",
        }
      ],
      "type": "com.model",
      "updatedTimestamp": "2021-03-24T17:53:52.997+0000"
    }
  }
]

Any insight on where I went astray is greatly appreciated; thank you in advance!


Solution

  • All - Thanks for anyone who was reviewing, but one of my team members figured it out...in case anyone ever happens across this issue, please see the update query that worked:

    UPDATE `NoSQLDB` AS c
    SET p.typeCode = “B_BUS” FOR p IN phones WHEN p.typeCode != ‘B_BUS’ END
    WHERE c.type=‘com.model’