Couchbase N1QL Query - Include outer document as parent

Gurmeet

I am new to Couchbase so need a little help in below n1ql query:

company:1
{
  'companyName':'A',
  'employees':[{
     'empId':101,
     'name':'Test',
     'deptId':'dept:1'
   },
   {
     'empId':102,
     'name':'Test2',
     'deptId':'dept:1'
   }]
}

dept:1
{
 'deptName':'Dept One',
 'location':'First Floor'
}

Output I need is like :

 company:1
    {
      'companyName':'A',
      'departments':[{
         'deptName':'Dept One',
         'location':'First Floor'
         'employees:[{
            'empId':101,
            'name':'Test',
            'deptId':'dept:1'
           },
           {
            'empId':102,
            'name':'Test2',
            'deptId':'dept:1'
           }]
       }],          
    }

I tried using Unnest and then using sub query but could not achieve the desired result. May be NEST can help but I dont have much idea, how to use NEST in n1ql.

Please help to achive the desired output.

vsr

First you unnest the employees and then group by company, department. Second you do group by company

SELECT d1.companyName, ARRAY_AGG(OBJECT_ADD(dep,"employees", d1.employees)) AS departments
FROM ( SELECT d.companyName, e.deptId, ARRAY_AGG(e) AS employees
       FROM default AS d
       UNNEST d.employees AS e
       WHERE d.type = "company"
       GROUP BY d.companyName, e.deptId) AS d1
LET dep = (SELECT RAW d3 FROM default AS d3 USE KEYS d1.deptId)[0]
GROUP BY d1.companyName;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Couchbase Java SDK: N1QL queries that include document id

From Dev

Couchbase N1QL query sum from sub document array

From Dev

Couchbase N1QL query aggregation

From Dev

Couchbase N1QL query with dictionary

From Dev

Upsert into sub document in couchbase using N1QL

From Dev

couchbase N1QL group-by in sub-document

From Dev

Couchbase n1ql - NEST foreign document using indexes

From Dev

Trouble with N1QL query and index in Couchbase

From Dev

Couchbase parameterized N1QL query IN statement

From Dev

Add count to N1QL query result in Couchbase

From Java

Couchbase java N1QL DSL query statement with IN expression

From

golang Couchbase n1ql query pass params to?

From Java

How to write Couchbase N1QL query in spring data?

From Dev

Why is there a syntax error - at 0 in this Couchbase N1QL query?

From Dev

Couchbase N1QL: index and query on array fields

From Dev

Start with Couchbase N1QL query in xampp

From Dev

Many to many relationship using N1ql couchbase query

From Dev

N1QL query count for each document of specific type

From Dev

how to retrieve the child of the parent in n1ql query

From Dev

Using IN clause in couchbase N1Ql @query or use findAll(keys) from couchbase JPA

From Dev

Couchbase N1QL Update complex-structured JSON document

From Dev

Couchbase N1QL: How to get complete Document including ID and CAS with SELECT

From Dev

Couchbase N1QL: Update document attribute based on another attribute

From Dev

How can I see the tree of a document in couchbase server in N1QL?

From Dev

Couchbase n1ql match any of a sub document properties value

From Dev

How to use the LENGTH() String Function in a N1QL Query (Couchbase Query)

From Dev

Couchbase N1QL query to select value from object array

From Dev

Can we execute N1QL query in exetrnal UDFs in couchbase?

From Java

How to use percent signs with LIKE in a custom Spring Boot N1QL query for use with a Couchbase DB

Related Related

  1. 1

    Couchbase Java SDK: N1QL queries that include document id

  2. 2

    Couchbase N1QL query sum from sub document array

  3. 3

    Couchbase N1QL query aggregation

  4. 4

    Couchbase N1QL query with dictionary

  5. 5

    Upsert into sub document in couchbase using N1QL

  6. 6

    couchbase N1QL group-by in sub-document

  7. 7

    Couchbase n1ql - NEST foreign document using indexes

  8. 8

    Trouble with N1QL query and index in Couchbase

  9. 9

    Couchbase parameterized N1QL query IN statement

  10. 10

    Add count to N1QL query result in Couchbase

  11. 11

    Couchbase java N1QL DSL query statement with IN expression

  12. 12

    golang Couchbase n1ql query pass params to?

  13. 13

    How to write Couchbase N1QL query in spring data?

  14. 14

    Why is there a syntax error - at 0 in this Couchbase N1QL query?

  15. 15

    Couchbase N1QL: index and query on array fields

  16. 16

    Start with Couchbase N1QL query in xampp

  17. 17

    Many to many relationship using N1ql couchbase query

  18. 18

    N1QL query count for each document of specific type

  19. 19

    how to retrieve the child of the parent in n1ql query

  20. 20

    Using IN clause in couchbase N1Ql @query or use findAll(keys) from couchbase JPA

  21. 21

    Couchbase N1QL Update complex-structured JSON document

  22. 22

    Couchbase N1QL: How to get complete Document including ID and CAS with SELECT

  23. 23

    Couchbase N1QL: Update document attribute based on another attribute

  24. 24

    How can I see the tree of a document in couchbase server in N1QL?

  25. 25

    Couchbase n1ql match any of a sub document properties value

  26. 26

    How to use the LENGTH() String Function in a N1QL Query (Couchbase Query)

  27. 27

    Couchbase N1QL query to select value from object array

  28. 28

    Can we execute N1QL query in exetrnal UDFs in couchbase?

  29. 29

    How to use percent signs with LIKE in a custom Spring Boot N1QL query for use with a Couchbase DB

HotTag

Archive