Sin categoría

Shopper or Merchant? Check-out N1QL Querying

Lectura de 3 minutos

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 productos, customers, y purchases.

First, lets get a list of productos belonging to a particular category  (in this case, I am looking for “appliances”)

  SELECT product

  DE product

  UNNEST product.categories as categories

  DÓNDE 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 :

“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”
   }
 ]
….

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

SELECT nombre || ” “ || Apellido as fullName, emailAddress

DE cliente

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

“resultset”: [
   {
     “emailAddress”: “zion@armstronghaley.biz”,
     “fullName”: “Rosella Tremblay”
   },
   {
     “emailAddress”: “kobe@douglas.net”,
     “fullName”: “Erich Toy”
   },
….

 

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

DE purchases KEY “purchase0” UNNEST purchases.lineItems CO elementos

ÚNETE product KEY elementos.product

ÚNETE cliente 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 Ventas. The following N1QL query calculates the sales month-over-month.

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

DE purchases unnest purchases.lineItems as elementos join product clave elementos.product

GRUPO POR substr(purchases.purchasedAt, 0, 7)

PEDIDO POR month

 

Want more?

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

Compartir este artículo

Autor

Don Pinto es un director principal de gestión de productos en Couchbase y actualmente se enfoca en avanzar las capacidades de Couchbase Server. Es un apasionado de la tecnología de datos y en el pasado ha escrito varios artículos sobre Couchbase Server, incluyendo blogs técnicos y documentos técnicos informativos (white papers). Antes de unirse a Couchbase, Don pasó varios años en IBM, donde desempeñó el rol de desarrollador de software en el grupo de gestión de información de DB2 y, más recientemente, como gerente de programas en el equipo de SQL Server en Microsoft. Don tiene una maestría en informática y una licenciatura en ingeniería informática de la Universidad de Toronto, Canadá.

Deja un comentario

¿Listo para comenzar con Couchbase Capella?

Comenzar a construir

Visita nuestro portal para desarrolladores para explorar NoSQL, consultar recursos y comenzar con los tutoriales.

Usa Capella gratis

Empieza a usar Couchbase en tan solo unos clics. Capella DBaaS es la forma más fácil y rápida de comenzar.

Ponte en contacto

¿Quieres saber más sobre las ofertas de Couchbase? Permítenos ayudarte.