0

我是 Couchbase 的新手,我得到了这个结构:

{ 
  items: [
    {
      id: "a1",
      subitems: [
        {
          id: "b1",
          name: "joe1"
        },
        {
          id: "b2",
          name: "joe2"
        }
      ] 
    }
  ],
  docType: "mydoc"   
}

我需要为子项内的每个对象添加新字段。我知道如何使用 1 级数组执行此操作 =>

UPDATE default SET a.newField='blabla' FOR a IN items END where docType = 'mydoc';

它将产生:

{ 
  items: [
    {
      newField: "blabla",
      id: "a1",
      subitems: [
        {
          id: "b1",
          name: "joe1"
        },
        {
          id: "b2",
          name: "joe2"
        }
      ] 
    }
  ],
  docType: "mydoc"   
}  

但我需要这个结果:

{ 
  items: [
    {
      id: "a1",
      subitems: [
        {
          newField: "blabla",
          id: "b1",
          name: "joe1"
        },
        {
          newField: "blabla",
          id: "b2",
          name: "joe2"
        }
      ] 
    }
  ],
  docType: "mydoc"   
}  

谢谢你的帮助。

4

1 回答 1

0

使用即将推出的 Couchbase 4.5.1,您应该能够:

UPDATE default
SET s.newField = 'newValue' FOR s IN ARRAY_FLATTEN(items[*].subitems, 1) END;

UPDATE default
SET s.newField = 'newValue' FOR s IN ARRAY_FLATTEN(ARRAY i.subitems FOR i IN items END, 1) END;

UPDATE default
SET i.subitems = ( ARRAY OBJECT_ADD(s, 'newField', 'blabla' ) FOR s IN i.subitems END ) FOR i IN items END;
于 2016-05-31T12:44:14.160 回答