Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News Using Language and Developer Friendly Data Structures with Couchbase

Using Language and Developer Friendly Data Structures with Couchbase

This item in japanese

The recently written Coachbase blog outlines how developers can use data structures (such as lists, maps, etc.) effectively with Couchbase 7.0, using the respective language APIs/SDKs.

Like most NoSQL databases, Couchbase was originally intended as a data store for key/value pairs and then for key/document (documents usually in a JSON or similar format). However, they can now be used with additional data structures making it more developer friendly. Most NoSQL databases have also evolved to support more language-specific data structures originally intended for simple key/value and key/document pair(s) scenarios, but the onus was on the programmer to covert from the respective data model to data structures and vice versa requiring to write a lot of boiler plate code.

Besides documents and counters, Couchbase language APIs now support the following foundational data structures.

  • Lists
  • Maps
  • Sets
  • Queues

Internally, Couchbase organizes the data structures as JSON documents, distributing it across the cluster and making it available as a language native data structure via the respective language APIs. Some examples below illustrate how to use data structures with the Couchbase Python SDK.

We will start by looking at some queue examples using Python. The Doc ID below is fibonacci and we push the values 0,1,1 respectively as illustrated below.

cb.queue_push("fibonacci", 0, create=True) # [0]
cb.queue_push("fibonacci", 0) # [0, 0]
cb.queue_push("fibonacci", 1) # [0, 0, 1]
cb.queue_size("fibonacci") # 3
cb.queue_pop("fibonacci") # 0 (in FIFO order)
cb.queue_pop("fibonacci") # 0
cb.queue_pop("fibonacci") # 1

Likewise, examples below show using the List APIs.

cb.list_append("qbs", "tom", create=True) # ["tom"]
cb.list_prepend("qbs", "josh") # ["josh","tom"]
cb.list_append("qbs", "patrick") # ["josh","tom", "patrick"]
cb.list_append("qbs", "aaron") # ["josh","tom", "patrick", "aaron"]
cb.list_size("qbs") # 4
cb.list_get("qbs", 0) # josh
cb.list_remove("qbs", 0) # ["tom", "patrick", "aaron"]
cb.list_get("qbs", 0) # tom
cb.list_get("qbs", 2) # aaron

The respective values can be JSON documents as illustrated below with the Map API.

cb.map_add("quarterbacks", "bills", {'name': 'Josh Allen', 'age': 24}, Create=True)
cb.map_add("quarterbacks", "bucs", {'name': 'Tom Brady', 'age': 43})
cb.map_add("quarterbacks", "chiefs", {'name': 'Patrick Mahomes', 'age': 25})
cb.map_add("quarterbacks", "packers", {'name': 'Aaron Rogers', 'age': 37})
cb.map_get("quarterbacks", "chiefs") # {'name': 'Patrick Mahomes', 'age': 25}


More data structure APIs for Python are available in the Data Structures docs and is summarized below.

Image Source: Couchbase Blog

Similar APIs are available for other language SDKs supported with Couchbase.

In summary, Couchbase and the accompanying language APIs makes it easier for developers to persist and retrieve data structures with the data structures APIs. A cheatsheet of the APIs and the docs are available on getting started and incorporating them in applications.

Rate this Article