0
0
PostgreSQLquery~3 mins

Why JSONB containment (@>) operator in PostgreSQL? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if you could instantly find any hidden detail inside messy JSON data with just one simple query?

The Scenario

Imagine you have a huge box full of mixed documents, each with different details, and you need to find all documents that contain a specific piece of information buried inside.

Without a smart tool, you would have to open each document and check every detail manually.

The Problem

Manually searching through each document is slow and tiring.

It's easy to miss details or make mistakes when checking many documents by hand.

Also, as the number of documents grows, the task becomes impossible to do quickly.

The Solution

The JSONB containment (@>) operator lets you quickly ask the database: "Show me all documents that contain this exact piece of information."

This means the database does the hard work of searching inside complex data structures for you, instantly and accurately.

Before vs After
Before
SELECT * FROM documents WHERE data::text LIKE '%"key": "value"%';
After
SELECT * FROM documents WHERE data @> '{"key": "value"}';
What It Enables

This operator makes it easy to filter and find complex nested data inside JSON documents with a simple, fast query.

Real Life Example

A company stores customer preferences as JSON data. Using the @> operator, they can quickly find all customers who prefer a certain product feature without scanning every detail manually.

Key Takeaways

Manually searching JSON data is slow and error-prone.

The @> operator lets the database efficiently find JSON documents containing specific data.

This makes querying complex JSON data fast, simple, and reliable.