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
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

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

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

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

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Designing a Blog Application Using Document Databases
  • Dynamic Watermarking of Bitmaps in Databases
  • How To Generate Scripts of Database Objects in SQL Server
  • How to Store Text in PostgreSQL: Tips, Tricks, and Traps

Trending

  • Data Quality: A Novel Perspective for 2025
  • Unlocking AI Coding Assistants: Generate Unit Tests
  • Agile’s Quarter-Century Crisis
  • Operational Principles, Architecture, Benefits, and Limitations of Artificial Intelligence Large Language Models
  1. DZone
  2. Data Engineering
  3. Databases
  4. Perform Various N1QL Queries Without Indexes in Couchbase Server

Perform Various N1QL Queries Without Indexes in Couchbase Server

You may be surprised to learn that not every N1QL query requires an index to exist. Learn how to run a few N1QL queries on a Couchbase Bucket that has no indexes.

By 
Nic Raboy user avatar
Nic Raboy
·
May. 03, 17 · Tutorial
Likes (0)
Comment
Save
Tweet
Share
6.0K Views

Join the DZone community and get the full member experience.

Join For Free

As you probably already know, you’re able to query Couchbase NoSQL documents using a SQL dialect called N1QL. This is made possible through indexes that you create on documents in your Couchbase Buckets. However, what if I told you that not every N1QL query requires an index to first exist? After talking with my colleague, Justin Michaels, he showed me an awesome trick to perform bulk operations in N1QL without indexes. This was news to me because I always thought you needed at least one index to exist, but hey, you learn something new every day.

We’re going to see how to run a few N1QL queries on a Couchbase Bucket that has no indexes and that includes no primary index.

Before we jump into some sample scenarios, you might be wondering how it is possible to run queries without an index. This is actually possible by making use of the USE KEYS operator to hone in on specific documents by their key that exists in the meta information.

Take the following document for example:

{
    "type": "person",
    "firstname": "Nic",
    "lastname": "Raboy",
    "social_media": [
        {
            "website": "https://www.thepolyglotdeveloper.com"
        }
    ]
}

Above, we have a simple document that represents a particular person. Let’s say the above document has nraboy as the ID value. To make things interesting, let’s create another document.

Assume the following has mraboy as the ID value:

{
    "type": "person",
    "firstname": "Maria",
    "lastname": "Raboy",
    "social_media": [
        {
            "website": "https://www.mraboy.com"
        }
    ]
}

So, if we wanted to query either of these two documents with the USE KEYS operator in N1QL, we could compose a query that looks like the following:

SELECT * 
FROM example 
USE KEYS ["nraboy", "mraboy"];

If you look at the EXPLAIN of the above query, you’ll notice that no index was used in the query. The above type of query would be useful if you knew the keys that you wanted to obtain and wanted incredibly fast performance similar to how it was done in a previous article I wrote, Getting Multiple Documents by Key in a Single Operation Eith Node.js.

Let’s make things a bit more complicated. What if we wanted to query with a relationship on one or more of the document properties?

Let’s create another document with couchbase as the document ID:

{
    "type": "company",
    "name": "Couchbase Inc",
    "address": {
      "city": "Mountain View",
      "state": "CA"
    }
}

The above document represents a company. As you probably guessed, we’re going to query for the company information of each person. To make this possible, let’s change the nraboy document to look like the following:

{
    "type": "person",
    "firstname": "Nic",
    "lastname": "Raboy",
    "social_media": [
        {
            "website": "https://www.thepolyglotdeveloper.com"
        }
    ],
    "company": "couchbase"
}

Notice we’ve added a property with the key to our other document. We won’t add any company information to the mraboy document.

Take the following query that has a multiple document relationship, but no indexes created:

SELECT
    p.firstname,
    p.lastname,
    (SELECT c.* FROM example c USE KEYS p.company)[0] AS company
FROM example p 
USE KEYS ["nraboy", "mraboy"];

Notice that the above query has a subquery that also uses the USE KEYS operator. Not bad right? Try using other operators like UNNEST to flatten the array data found in the social_media property.

Conclusion

You just saw how to write N1QL queries in Couchbase that use no index. By using the USE KEYS operator we can do bulk operations based on key, like I demonstrated in the articles Getting Multiple Documents by Key in a Single Operation With Node.js and Using Golang to Get Multiple Couchbase Documents by Key in a Single Operation. A huge thanks to Justin Michaels from Couchbase for helping me with this.

Database Document Couchbase Server

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

Opinions expressed by DZone contributors are their own.

Related

  • Designing a Blog Application Using Document Databases
  • Dynamic Watermarking of Bitmaps in Databases
  • How To Generate Scripts of Database Objects in SQL Server
  • How to Store Text in PostgreSQL: Tips, Tricks, and Traps

Partner Resources

×

Comments

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: