couchbase Creating document with TTL (Time To Live)


TTL value can be used to decide for how long the document needs to be there in the bucket. By default TTL value is 0, which means it will be there for indefinite time period.

String bucketName = "bucket";
List<String> nodes = Arrays.asList("node1","node2"); // IP or hostname of one or more nodes in the cluster

Cluster cluster = CouchbaseCluster.create(nodes);
Bucket bucket = cluster.openBucket(bucketName);
//create the document with id 123 and TTL 1seconds
bucket.insert(JsonDocument.create("123",JsonObject.empty(), 1)); //if TTL is 0 document will be there in the DB for indefinite time
//do other stuffs
//to update the TTL 
bucket.upsert(JsonDocument.create("123",JsonObject.empty())); //no TTL value is provided