What if you could instantly find any hidden detail inside messy JSON data with just one simple query?
Why JSONB containment (@>) operator in PostgreSQL? - Purpose & Use Cases
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.
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 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.
SELECT * FROM documents WHERE data::text LIKE '%"key": "value"%';SELECT * FROM documents WHERE data @> '{"key": "value"}';This operator makes it easy to filter and find complex nested data inside JSON documents with a simple, fast query.
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.
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.
Practice
What does the PostgreSQL @> operator do when used with JSONB data?
Solution
Step 1: Understand the operator's purpose
The@>operator in PostgreSQL is used to check containment between JSONB values.Step 2: Identify the direction of containment
It checks if the JSONB on the left side contains the JSONB on the right side.Final Answer:
Checks if the left JSONB contains the right JSONB -> Option DQuick Check:
JSONB containment = left contains right [OK]
- Confusing which side contains which
- Thinking it merges JSONB objects
- Assuming it deletes data
Which of the following is the correct syntax to check if a JSONB column data contains the JSON {"key": "value"}?
SELECT * FROM table WHERE data ___ '{"key": "value"}'::jsonb;Solution
Step 1: Identify the containment operator
The@>operator checks if the left JSONB contains the right JSONB.Step 2: Apply correct syntax
To check ifdatacontains the JSON, usedata @> '{"key": "value"}'::jsonb.Final Answer:
@> -> Option AQuick Check:
Containment operator = @> [OK]
- Using <@ which reverses containment
- Using -> or ->> which access keys, not contain
- Forgetting to cast string to jsonb
Given the table items with a JSONB column attributes, what will this query return?
SELECT id FROM items WHERE attributes @> '{"color": "red"}'::jsonb;Assuming items contains:
id | attributes
---+-------------------------
1 | {"color": "red", "size": "M"}
2 | {"color": "blue", "size": "L"}
3 | {"color": "red"}
4 | {"size": "S"}Solution
Step 1: Understand the query condition
The query selects rows whereattributescontain the JSON{"color": "red"}.Step 2: Check each row's attributes
Rows 1 and 3 have "color": "red"; rows 2 and 4 do not.Final Answer:
[1, 3] -> Option CQuick Check:
Only rows with color red = [1, 3] [OK]
- Including rows without the key-value pair
- Confusing @> with key existence operator
- Ignoring the JSONB cast requirement
Identify the error in this query that tries to find rows where info JSONB column contains {"active": true}:
SELECT * FROM users WHERE info @> '{active: true}';Solution
Step 1: Check JSON syntax correctness
JSON keys must be enclosed in double quotes, soactiveshould be"active".Step 2: Verify casting and operator
The query is missing::jsonbcast, but the main syntax error is the missing quotes around the key.Final Answer:
JSON keys must be in double quotes -> Option BQuick Check:
JSON keys need double quotes [OK]
- Forgetting double quotes around keys
- Not casting string to jsonb
- Using wrong containment operator
You want to find all rows in products where the details JSONB column contains {"features": {"waterproof": true}} and also the price is less than 100. Which query is correct?
Solution
Step 1: Use correct containment operator and JSON syntax
The@>operator checks ifdetailscontains the given JSON. The JSON must have keys in double quotes and be cast to jsonb.Step 2: Combine with price condition
UseAND price < 100to filter by price.Final Answer:
SELECT * FROM products WHERE details @> '{"features": {"waterproof": true}}'::jsonb AND price < 100; -> Option AQuick Check:
Correct operator, JSON syntax, and price filter [OK]
- Using <@ instead of @>
- Missing quotes in JSON keys
- Incorrect JSONB casting
- Trying to use -> operator for containment
Ready to prove it?
Pass the test and this pattern moves unknown
