Search code examples
couchbasesql++

Include document id on Couchbase Nest right hand side array


I have a nest operation that joins an array of ids with some documents.

SELECT left.*,right FROM bucket AS left 
  LEFT NEST bucket AS right 
  ON META(right).id IN left.array

Result

[
  {
    array : ["rightId1","rightId2"],
    right : [ { < rightFields1 > }, { < rightFields2 > } ]
  }
]

I need the returned documents (< rightFields >) have the id on one of its fields, how can I do this?


Solution

  • I don't know if there is an easier solution, but I solved it by iterating each item of the array with ARRAY, and adding it a new property with OBEJECT_ADD

    SELECT left.*,ARRAY OBJECT_ADD(item, "id", META(item).id) FOR item IN right END 
      FROM bucket AS left 
      LEFT NEST bucket AS right 
      ON META(right).id IN left.array