Couchbase N1ql查询更新新的objet到JSON数组

时间:2017-08-24 06:01:30

标签: couchbase n1ql

我有以下示例JSON

"address":
{
  "details": [
    {
      "id": 1234,
      "city": "Medway",
      "country": "United Kingdom",
      "name": "Medway Youth Hostel",
      "url": "http://www.yha.org.uk"
    }]
}

我希望数据有以下几种方式 例1:

"address":
{
  "details": [
    {
      "id": 1234,
      "city": "Medway",
      "country": "United Kingdom",
      "name": "Medway Youth Hostel",
      "url": "http://www.yha.org.uk",
      "contact": [
        {
          "email": "hari34@gmail.com",
          "phone": 6789435634
        }
          ]
    }]
}  

示例2:

"address":
{
  "details": [
    {
      "id": 1234,
      "city": "Medway",
      "country": "United Kingdom",
      "name": "Medway Youth Hostel",
      "url": "http://www.yha.org.uk",
      "contact": [
        {
          "email": "hari34@gmail.com",
          "phone": 6789435634
        },
        {
          "email": "sri123@gmail.com",
          "phone": 9876655555
        }
          ]
    }]
}

示例3:

"address":
{
  "details": [
    {
      "id": 1234,
      "city": "Medway",
      "country": "United Kingdom",
      "name": "Medway Youth Hostel",
      "url": "http://www.yha.org.uk",
      "contact": [
        {
          "email": "hari34@gmail.com",
          "phone": 6789435634
        }
      ]
    },
    {
      "id": 3456,
      "city": "Texas",
      "country": "USA",
      "name": "Texas Youth Hostel",
      "url": "http://www.yha.org.uk",
      "contact": [
        {
          "email": "ram123@gmail.com",
          "phone": 876543219
        }
      ]
    }
  ]
}

示例4:

 this.tempForm = this.fb.group({
  m26_type:  '',
  m26_name:  ''
 })
this.tempForm.get('m26_type').disable();

我对n1ql查询很新,请有人帮我找到插入/更新数组到对象的对象的解决方案。示例1是将数据插入JSON,示例2是将数据更新为示例1,然后是...... 1.请帮助获取满足上述示例的n1ql查询。 2.如何在文档中生成UUID到id字段

1 个答案:

答案 0 :(得分:0)

1)追加数组永远不会检查数组中是否已存在元素

.test

2)USE UUID()函数如下所示

INSERT INTO default VALUES ("k01", { "details": [ { "id": 1234, "city": "Medway", "country": "United Kingdom", "name": "Medway Youth Hostel", "url": "http://www.yha.org.uk" }] });

UPDATE default AS d SET e.contact = [ { "email": "hari34@gmail.com", "phone": 6789435634 } ]
                           FOR e IN d.details WHEN e.id = 1234 END
WHERE META().id = "k01";

UPDATE default AS d SET e.contact = ARRAY_APPEND(e.contact,{ "email": "sri123@gmail.com", "phone": 9876655555 })
                           FOR e IN d.details WHEN e.id = 1234 END
WHERE META().id = "k01";

UPDATE default AS d SET d.details = ARRAY_APPEND(d.details,{ "id": 3456, "city": "Texas", "country": "USA", "name": "Texas Youth Hostel", "url": "http://www.yha.org.uk", "contact": [ { "email": "ram123@gmail.com", "phone": 876543219 } ] })
WHERE META().id = "k01";

SELECT * from default USE KEYS["k01"];