Hello there.
I’m coming from a postgresql background where we have a “DEFAULT” keyword that will automatically insert your record with the next available number… assuming your table was created using a PK that’s set to auto increment.
I’m wondering how to do something similar with couchbase. I’ve manually added some documents that are called “audit_1” and “audit_2” but I’d like a way to programmatically assign these document names. Or maybe I need a complete paradigm shift. ??
If you leverage some of the KV capabilities along with N1QL, you can use an atomic ‘counter’ document as a place to maintain a sequence. It’s a bit different in that you’ll increment it first, but the cost is fairly cheap.
One other technique is to simply increment the number in the application and use the KV insert which will fail if the document already exists. This one is a bit more costly since if you have many PHP application servers and a high creation rate, you will use a lot of network IO. It’s fairly inexpensive on the server in processing, so this is a valid technique in situations where network IO is low-latency/high-throughput.