1

I have a document having nested structure like given below

{

"docType": "account",
"accounts": [
    {
        "id": "123123",
        "name": "abcdCompany",
        "owner": "abcdCompany corporation",
        "email": "[email protected]",
        "projects": [
            {
                "id": "1",
                "name": "abcdCompany asset management",
                "owner": "assetMgmt",
                "email": "[email protected]"
            },
            {
                "id": "2",
                "name": "abcdCompany alert notification",
                "owner": "alertNotification",
                "email": "[email protected]"
            }
        ]
    }
]
}

How we can get project having "owner": "alertNotification"

5
  • Have you looked into CouchDB views? Commented Apr 28, 2020 at 13:02
  • @Rishabh are you using Couchbase or are you using CouchDB? They are not the same, so the answer will be very different. If you are using Couchbase, do you want to use N1QL? Commented Apr 28, 2020 at 13:24
  • 1
    @MatthewGroves its Couchbase, yes if you suggest both type of queries with and without N1QL Commented Apr 28, 2020 at 13:36
  • 1
    Are you using spring data couchbase? The tag has been removed. Commented Apr 28, 2020 at 18:18
  • Sorry, I removed it, because this question didn't seem related to spring. Feel free to add it back if it's relevant! Commented Apr 29, 2020 at 13:59

1 Answer 1

4

I'm not exactly sure what you have in mind (you may want to check out this SO question for details about UNNEST vs ANY/SATISFIES), but if you want to just select the nested project objects where owner == 'alertNotification', you can use UNNEST. You've got an array within an array, so you'd need to UNNEST twice:

SELECT prj.*
FROM moviegame b
UNNEST b.accounts acct
UNNEST acct.projects prj
WHERE prj.owner == 'alertNotification';

That would return:

[
  {
    "email": "[email protected]",
    "id": "2",
    "name": "abcdCompany alert notification",
    "owner": "alertNotification"
  }
]

If you DON'T want to UNNEST, and you just want to return any document which has an account which has a project which has an 'owner' of 'alertNotification', then you can use ANY/SATISFIES (again nested, because there's an array within an array):

SELECT b.*
FROM moviegame b
WHERE ANY a IN accounts SATISFIES (ANY p IN a.projects SATISFIES p.owner == 'alertNotification' END) END

which would return the whole document in your sample (below) but would NOT include other documents that do NOT have a project with owner=='alertNotification' within them:

[
  {
    "accounts": [
      {
        "email": "[email protected]",
        "id": "123123",
        "name": "abcdCompany",
        "owner": "abcdCompany corporation",
        "projects": [
          {
            "email": "[email protected]",
            "id": "1",
            "name": "abcdCompany asset management",
            "owner": "assetMgmt"
          },
          {
            "email": "[email protected]",
            "id": "2",
            "name": "abcdCompany alert notification",
            "owner": "alertNotification"
          }
        ]
      }
    ],
    "docType": "account"
  }
]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.