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 Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
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
Partner Zones AWS Cloud
by AWS Developer Relations
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
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Data Engineering
  3. Databases
  4. N1QL Querying for Shoppers and Merchants
Content provided by Couchbase logo

N1QL Querying for Shoppers and Merchants

Don Pinto user avatar by
Don Pinto
·
Apr. 17, 14 · Interview
Like (1)
Save
Tweet
Share
3.06K Views

N1QL is a next generation query language for Couchbase Server. It goes beyond SQL and the relational model in several ways - most importantly, attributes in N1QL can contain multiple values, and these values can be nested. In this blog, we will go over some N1QL queries that are commonly seen in an e-commerce application. This kind of app would have a variety of rich data related to products, customers, and purchases.

First, let's get a list of products belonging to a particular category  (in this case, I am looking for “appliances”)

 SELECT product

 FROM product

 UNNEST product.categoriesascategories

WHERE categories="Appliances"

I’ve now found an appliance that I’m looking to buy, but before I check-out, I need to log into the store. Your app can store user-profiles in Couchbase in the form of JSON documents as shown below :

<span>"resultset": [
   {
     "ccInfo": {
       "cardExpiry": "2013-09-12",
       "cardNumber": "1228-1221-1221-1431",
       "cardType": "discover"
     },
     "customerId": "customer0",
     "dateAdded": "2013-06-23T04:32:31Z",
     "dateLastActive": "2013-07-23T04:32:31Z",
     "emailAddress": "zion@armstronghaley.biz",
     "firstName": "Rosella",
     "lastName": "Tremblay",
     "phoneNumber": "1-543-962-9861 x534",
     "postalCode": "75832",
     "state": "PR",
     "type": "customer"
   }
 ]
....</span></span>

Now, lets get a list of customers and their e-mail addresses. To do that, you can use the following N1QL query :

SELECT firstName||" "||lastName as fullName, emailAddress

FROM customer

|| is used to concatenate strings in N1QL.  The result of this query will be something like the follows -

<span>"resultset": [
   {
     "emailAddress": "zion@armstronghaley.biz",
     "fullName": "Rosella Tremblay"
   },
   {
     "emailAddress": "kobe@douglas.net",
     "fullName": "Erich Toy"
   },
....
</span></span>

After I login, I add the item to my shopping cart. Before I check-out, I add a few more items that I like into my shopping cart. I then submit my order. Soon later, the dispatch team is notified that an order is placed and would like to review my purchase order to prepare the package to be mailed out.

The following N1QL query, will prepare the shopping purchase order for “purchase0”
 

SELECT purchases, product, customer

FROM purchases KEY "purchase0" UNNEST purchases.lineItems AS items

JOIN product KEY items.product

JOIN customer KEY purchases.customerId

Here the document that has purchase information is joined with the product and customer documents to get complete purchase order information.

At the end of the year, the store wants to review its annual sales. The following N1QL query calculates the sales month-over-month.

SELECT substr(purchases.purchasedAt, 0, 7) asmonth, round(sum(product.unitPrice*items.count)/1000000, 3) as revenueMillion

FROM purchasesunnestpurchases.lineItemsasitemsjoinproductkeyitems.product

GROUP BY substr(purchases.purchasedAt, 0, 7)

ORDER BY month

Want more?

We only skimmed over a few N1QL queries related to e-commerce. To learn more about N1QL, check out  http://query.couchbase.com and don’t forget to register for our upcoming N1QL webinar to go over this use-case in more detail.


Comments

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: