How do I auto increase document ID with c lib?


Hi, guys:
I’m trying to add doc by lcb_store, is there any way to auto increase doc id so I don’t have to generate unique doc id in my code? right now, i need to provide _key which will be stored as doc id.
bool CouchbaseHandler::addData(string _key,string _value)

lcb_store_cmd_t store;
memset(&store, 0, sizeof(store));
store.version = 0;
store.v.v0.key = _key.c_str();
store.v.v0.nkey = strlen((const char*)(store.v.v0.key));
store.v.v0.bytes = _value.c_str();
store.v.v0.nbytes = strlen((const char*)(store.v.v0.bytes));
store.v.v0.operation = LCB_ADD;
const lcb_store_cmd_st* const store_commands[1] = { &store };
lcb_error_t err;
err = lcb_store(instance, NULL, 1, store_commands);
if (err != LCB_SUCCESS) {
fprintf(stderr, “failed to add: %s\n”,
lcb_strerror(NULL, err));
return false;
return true;


You can use lcb_arithmetic with increment operation


how to use lcb_arithmetic to add new document with auto increment ID? can you provide sample code? thanks!


you can find examples in manpages (man lcb_arithmetic) or on the web


It isn’t possible to get the same value from server using increment


I meant you need to increment value before setting new key, and use this incremented value to construct new key. Or use can use any of uuid generation techniques


thanks a lot!
To achieve my goal i need to do following steps:

  1. increment value for document “counter” with lcb_arithmetic_cmd_t
  2. get the increamented value
  3. use the increamented value as new key and create new docuemnt

however it seems libcouchbase is NOT thread safety

if I use instance pool similar to pillowfight, create thread for each instance. is it possible that 2 instances ends up get the same new key? thanks!


"lcb_arithmetic() is used to perform a arithmetic operations on a
document’s value. "
I’m actually asking how to auto increase document id which is the key of document. MySQL support auto increament key:

does couchbase has similar API?