Filtering document databases with Postgres JSON
Using Postgres JSON, data stored in documents can be queried and filtered using the ->
and ->>
operators. To practice, you'll filter reviews using Postgres JSON. Similar to before, the nested_reviews
table takes the form below, and a sqlalchemy
connection object has been configured, and made available for you via the db_engine
variable. pandas
has also been loaded as pd
.
This exercise is part of the course
Introduction to NoSQL
Exercise instructions
- Use Postgres JSON to retrieve the value stored at the
statement
key in thereview
column, for each record in thenested_reviews
table. - Only return results with a
branch
nested in thelocation
object of thereview
column equal to'Disneyland_California'
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Build the query to select the rid and rating fields
query = """
SELECT
review ____ '____' AS customer_review
FROM nested_reviews
WHERE review ____ '____' ____ '____' = 'Disneyland_California';
"""
# Execute the query, render results
data = pd.read_sql(query, db_engine)
print(data)