How to generate a unique auto increment number in a collection

This forum contains topics that were moved from Get Satisfaction
araskin3i
Posts: 0
Joined: Fri Dec 20, 2013 2:44 pm

How to generate a unique auto increment number in a collection

Post by araskin3i »

Everytime that I write to a collection in my DB, I want to generate an auto-increment value for the record. I dont see how this can be done in the DB or using JS. Any ideas on how to auto increment a field in the DB?

Kateryna Grynko
Posts: 0
Joined: Thu Nov 15, 2012 9:13 am

How to generate a unique auto increment number in a collection

Post by Kateryna Grynko »

Hi Alon,

Unfortunately this can't be done. As a workaround you can create a service that reads maximum field value, and then increment it before writing the next value to Database.

araskin3i
Posts: 0
Joined: Fri Dec 20, 2013 2:44 pm

How to generate a unique auto increment number in a collection

Post by araskin3i »

Sorry to re-open this thread but I wanted to see if there was any update on this?

I need to provide an OrderId when the 'Create Order' Service is called. It should be a 5 or 6 digit number.

Even if we took the approach specified above isn't it still possible that two different calls to the same service will try to read the same number and therefore incrementing it will create two orders with the same number? The act of reading and incrementing should be handled on the database (atomic operation), should it not?

Evgene Karachevtsev
Posts: 12
Joined: Mon Apr 28, 2014 1:12 pm

How to generate a unique auto increment number in a collection

Post by Evgene Karachevtsev »

Hello,

Yes, situation you've described is possible, though its probability is extremely low. Unfortunately other variation do not implemented. There is workaround:
1) You may use _id - it is unique (though the length is more than you need).
2) After recording OrderId you may check whether there are any duplicates in the database and roll back the operation

Daniel Wong
Posts: 0
Joined: Sat Oct 11, 2014 7:47 pm

How to generate a unique auto increment number in a collection

Post by Daniel Wong »

I want to do exactly the same thing, and I come across how it can be down with MongoDB. My question is whether it is possible to access the DB with server code, so that we can implement a REST service to support this? Or there is a easier way to do this? This is such a common feature for any reasonable application, can we have some more sample code for it? Many thanks!

http://docs.mongodb.org/manual/tutori...

Kateryna Grynko
Posts: 0
Joined: Thu Nov 15, 2012 9:13 am

How to generate a unique auto increment number in a collection

Post by Kateryna Grynko »

Hi Daniel,

You can not access mongoDB directly from servercode so you would need a REST API: http://devcenter.appery.io/documentat...

However this should be enough to implement Optimistic Loop approach. You can do without the server code, since it has limitations of the execution time and interval between runs.

That is, you get the current maximum id. Increase it by 1, try to add a record with this id. Check how many records with this id are in the collection, if more than one, then delete your account and try to re-add the account after a while (setTimeout). Time interval before the next attempt should be assigned randomly: https://developer.mozilla.org/en-US/d...
(say from a few milliseconds to 1-2 seconds).

Enfiled Riders
Posts: 0
Joined: Fri Jun 05, 2015 6:07 am

How to generate a unique auto increment number in a collection

Post by Enfiled Riders »

Hey is there any update on this?

I want a alpha numeric Order id.
The orderid in the database for every document is extremely long and not feasible to use.

Dhananjay Mahajan
Posts: 0
Joined: Thu Apr 30, 2015 9:24 am

How to generate a unique auto increment number in a collection

Post by Dhananjay Mahajan »

Hi

This is what i do. In the UserData collection i have a seed starting number for every user code.
I generate a concatenation of userid and this number to create a unique alphanumeric order id. (of course with every order the Userdata record is updated to get the next number). Caution : This may create some holes in the order ids sequence under some exception conditions otherwise this works well

hope it helps

first
Posts: 0
Joined: Fri Nov 06, 2015 6:50 pm

How to generate a unique auto increment number in a collection

Post by first »

I want to insert new record with incrementing counter field using REST api provided by mongolab.

Code: Select all

  ex:  

Post-method REST call 1:

POST /api/1/databases/thedatabase/collections/Member?apiKey=xXxxxxXXXxxxxXXXxxxxXXXXxxxXXXXxxX HTTP/1.1
Host: api.mongolab.com
Content-Type: application/json
Cache-Control: no-cache
Postman-Token: 4c66f955-758a-81e4-484e-89408d643eba
{
"name":"Jack"
}

Post-method REST call 2:

POST /api/1/databases/thedatabase/collections/Member?apiKey=xXxxxxXXXxxxxXXXxxxxXXXXxxxXXXXxxX HTTP/1.1
Host: api.mongolab.com
Content-Type: application/json
Cache-Control: no-cache
Postman-Token: 4c66f955-758a-81e4-484e-89408d643eba
{
"name":"John"
}

I want this to effect as { "id": 1, "name":"Jack" } , { "id":2, "name":"John" }

Tried using $inc operator but not working.

Anybody with solution?

Serhii Kulibaba
Posts: 147
Joined: Tue Aug 27, 2013 1:47 pm

How to generate a unique auto increment number in a collection

Post by Serhii Kulibaba »

Unfortunately this is something outside the scope of standard Appery.io platform support. You may consider purchasing Advisory Pack to get more in-depth help on this question. Here is more information about Advisory Pack (http://appery.io/services/#Advisory_Pack).

Post Reply