Create Documents

  • Capella Operational
  • how-to
March 16, 2025
+ 12
How to create documents with a command line tool or an SDK.

Introduction

Couchbase stores its data as either JSON or Binary documents with unique IDs (primary keys) by which they can be located.

Documents are organized and grouped in buckets using scopes and collections. If a scope or collection is not provided when creating a document, Couchbase will automatically store data in the _default collection or _default scope.

Read the following for further information about the clients available:

Please note that the examples in this guide will alter the data in your sample database. To restore your sample data, remove and reinstall the travel sample data. Refer to Import Data with the Capella UI for details.

Inserting a Document

To create a single document, perform an insert operation.

  1. If you haven’t already done so, use cb-env to set the bucket, scope, and collection in which to store the document.

  2. Create a structured JSON object containing your data.

  3. Use doc insert to create the document.


The example below inserts a new JSON document in the hotel keyspace in the inventory scope. The document’s ID is hotel-123.

cb-env bucket travel-sample
cb-env scope inventory
cb-env collection hotel

doc insert hotel-123 {
  "id": 123,
  "name": "Medway Youth Hostel",
  "address": "Capstone Road, ME7 3JE",
  "url": "http://www.yha.org.uk",
  "geo": {
    "lat": 51.35785,
    "lon": 0.55818,
    "accuracy": "RANGE_INTERPOLATED"
  },
  "country": "United Kingdom",
  "city": "Medway",
  "state": null,
  "reviews": [
    {
      "content": "This was our 2nd trip here and we enjoyed it more than last year.", 
      "author": "Ozella Sipes",
      "date": "2021-11-17T17:35:05.351Z"
    }
  ],
  "vacancy": true,
  "description": "40 bed summer hostel about 3 miles from Gillingham."
}
Result
console
╭───┬───────────┬─────────┬────────┬──────────┬─────────╮ │ # │ processed │ success │ failed │ failures │ cluster │ ├───┼───────────┼─────────┼────────┼──────────┼─────────┤ │ 0 │ 1 │ 1 │ 0 │ │ capella │ ╰───┴───────────┴─────────┴────────┴──────────┴─────────╯
If the document already exists, Couchbase Shell returns a Key already exists error.

For further details, refer to Mutating in the Couchbase Shell documentation.

When a document is created, Couchbase assigns it a CAS (Compare And Swap) value to keep track of the document’s state within the database. Each time a document is mutated the CAS will change accordingly. This unique value allows the database to protect against concurrent updates to the same document.

Inserting with Options

To specify further parameters for the inserted document, such as expiry, add the options to the insert operation.

  1. If you haven’t already done so, use cb-env to set the bucket, scope, and collection in which to store the document.

  2. Create a structured JSON object containing your data.

  3. Use doc insert to create the document.

  4. Pass any required options, such as --expiry.


The example below inserts a new JSON document and sets it to expire after 60 seconds. The document will be automatically deleted once expired.

cb-env bucket travel-sample
cb-env scope inventory
cb-env collection hotel

doc insert --expiry 60 hotel-456 {
  "id": 456,
  "title": "Ardèche",
  "name": "La Pradella",
  "address": "rue du village, 07290 Preaux, France",
  "phone": "+33 4 75 32 08 52",
  "url": "http://www.lapradella.fr", 
  "country": "France",
  "city": "Preaux",
  "state": "Rhône-Alpes",
  "vacancy": false
}
Result
console
╭───┬───────────┬─────────┬────────┬──────────┬─────────╮ │ # │ processed │ success │ failed │ failures │ cluster │ ├───┼───────────┼─────────┼────────┼──────────┼─────────┤ │ 0 │ 1 │ 1 │ 0 │ │ capella │ ╰───┴───────────┴─────────┴────────┴──────────┴─────────╯

For further details, refer to Mutating in the Couchbase Shell documentation.

Key-Value Operations with SDKs: