DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • NoSQL for Relational Minds
  • Vector Databases Are Reinventing How Unstructured Data Is Analyzed
  • Architecture and Code Design, Pt. 2: Polyglot Persistence Insights To Use Today and in the Upcoming Years
  • Architecture and Code Design, Pt. 1: Relational Persistence Insights to Use Today and On the Upcoming Years

Trending

  • Infrastructure as Code (IaC) Beyond the Basics
  • Designing a Java Connector for Software Integrations
  • Strategies for Securing E-Commerce Applications
  • When Airflow Tasks Get Stuck in Queued: A Real-World Debugging Story
  1. DZone
  2. Data Engineering
  3. Data
  4. Flattening and Querying NoSQL Array Data With N1QL

Flattening and Querying NoSQL Array Data With N1QL

Need to query within embedded documents? This example takes us through using Couchbase's N1QL query for objects in a nested array in a single document.

By 
Nic Raboy user avatar
Nic Raboy
·
Mar. 02, 17 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
12.5K Views

Join the DZone community and get the full member experience.

Join For Free

I was browsing the Couchbase forums and I came across a question regarding queries against array data in Couchbase. Coming from a relational database, I too once struggled to grasp the concept of querying complex formatted JSON data with SQL.

How do you query within these embedded NoSQL documents? There are numerous ways, none of which are particularly difficult. We’re going to examine some of the complex query possibilities.

In case you’re curious about the question I stumbled upon, it can be found here. The user wanted to know how to query for objects that were nested in an array for a single document. The proposed document model looked similar to this:

{
  "id": "order-1",
  "type": "order",
  "items": [
    {
      "id": "pokemon-blue",
      "type": "gaming",
      "name": "Pokemon Blue"
    },
    {
      "id": "ms-surface-book",
      "type": "computing",
      "name": "Microsoft Surface Book"
    }
  ]
}


The end goal was to be able to get each object in a query based on a WHERE condition that included the nested type property.

One way to do this is to write a N1QL query that looks like the following:

SELECT 
    forum.id, forum.type, item
FROM forum
UNNEST items AS item
WHERE item.type != "computing";


In the above query, we are performing a SELECT from a Couchbase Bucket called forum and flattening the array using the UNNEST keyword. The flattened result set would look like the following before applying the WHERE condition:

[
  {
    "id": "order-1",
    "item": {
      "id": "pokemon-blue",
      "name": "Pokemon Blue",
      "type": "gaming"
    },
    "type": "order"
  },
  {
    "id": "order-1",
    "item": {
      "id": "ms-surface-book",
      "name": "Microsoft Surface Book",
      "type": "computing"
    },
    "type": "order"
  }
]


The WHERE condition will return us a single result instead of two, where the single result is of a gaming type as per our query.

So is this the only way to accomplish what we’ve just done? Absolutely not!

Take the following N1QL query in Couchbase:

SELECT 
    forum.id, 
    forum.type, 
    ARRAY item FOR item IN forum.items WHEN item.type != 'computing' END AS item
FROM forum


In the above query, we are not first flattening the array through an UNNEST operation. Instead, we are using one of the collection operators to find array items that meet our criteria.

Are there other ways to get the job done? Of course there are, but these two should be enough to get you started when it comes to querying arrays in Couchbase with N1QL.

If you need more help with N1QL, check out the Couchbase Developer Portal for other examples.

Data structure Database Data (computing) Relational database NoSQL

Published at DZone with permission of Nic Raboy, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • NoSQL for Relational Minds
  • Vector Databases Are Reinventing How Unstructured Data Is Analyzed
  • Architecture and Code Design, Pt. 2: Polyglot Persistence Insights To Use Today and in the Upcoming Years
  • Architecture and Code Design, Pt. 1: Relational Persistence Insights to Use Today and On the Upcoming Years

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!