Collection projection

n1ql
.net

#1

How can I project a nested object in lookupIn and n1ql ?

I the following document

{
  "foo":{
    "bar":{
      "a":1,
      "b":2,
      "c":3
    },
    "baz":{
      "a":4,
      "b":5
    }
  }
}

How we can select foo.*.a ?

{
  "foo":{
    "bar":{
      "a":1
    },
    "baz":{
      "a":4
    }
  }
}

#2

You can’t use * in the middle. you need to provide explicit name or Use * at the end


#3

I know, but how can I select it??( project a field)

I have a map of objects , my objects has large parts, I just need to select some fields


#4

You need to construct Object according to requirements.

   insert into default values("1",{ "bar":{ "a":1, "b":2, "c":3 }, "baz":{ "a":4, "b":5 } }); 
   SELECT OBJECT v.name: {v.val.a} FOR v IN  OBJECT_PAIRS(d)  END AS obj 
    FROM default d;

#5

What about bucket.lookupIn.get?


#6

I am not expert in that area cc @ingenthr. Most probably you need to get whole document and select in SDKs or use subdoc API to get specific paths.


#7

@vsr1 I found that foo[*].a works for arrays, but I don’t found for objects
Where is foo[*].a document? I found it by try and fail


#8
If array you can use foo[*].a because it subscripts, not for objects which needs fields.

https://docs.couchbase.com/server/6.0/n1ql/n1ql-language-reference/arrayfun.html#fn-array-star

#9

The Sub-Document API cannot do this iteration and processing. I believe it may be possible to do this with JSONPath. You can probably also build an index inclusive of projection in this case with Couchbase Views in javascript.