Auto Increment Id

We’re migrating an application to .net core(3.1) and the existing data of the application would be migrated to Couchbase(SDK .Net 2.7, server 6.0.4).

Let’s consider there are 5000 records that need to be migrated to Couchbase with the last record having a recordId “5000”.

We want to be able to use this number and auto-increment it by one when a new record is added after the migration is done, i.e. the first record added after the migration, should have a recordId “5001”, the next should have “5002”.

We’re using the bucket.Increment method to achieve this, however, it returns value “1” by default on first Increment, even when there are migrated records with a recordId value greater than zero.

“this.bucket.Increment(“recordId”, 1).Value.ToString()”

Is there a way to achieve this?


The way that bucket.Increment is by creating/modifying a document with the given ID (e.g. recordId) that has an integer value in it. If there isn’t a recordId document to begin with, that would explain why it’s returning 1. Try creating a document with a key of “recordId” and putting an integer value of 5000 in it. The next bucket.Increment(“recordId”, 1) should then return 5001, and so on.

Hi @matthew.groves,

Thank you for responding.

We implemented it exactly how you suggested, I created a document with recordId set to 5000 and then tried incrementing it.
It still started at 1.

Could you please help us point in the right direction?

I just ran through an example locally. Here’s the “recordId” document I started with (I just created this manually).


Then, here’s the code I ran in a console app (Using Couchbase .NET SDK 2.7.24):

    var bucketName = "matt";

    var cluster = new Cluster(new ClientConfiguration
        Servers = new List<Uri> {new Uri("http://localhost:8091")}
    cluster.Authenticate(new PasswordAuthenticator("Administrator", "password"));

    var bucket = await cluster.OpenBucketAsync(bucketName);

    await bucket.IncrementAsync("recordId", 1);


Finally, here’s the “recordId” document after I ran the above code.


1 Like

Hi @matthew.groves ,

Thanks to your example I could figure out the issue(the document was storing the value in key/value format).
Thanks a ton!

Have a good day!
Stay Safe!

1 Like