Page 1 of 1

How to generate a unique auto increment number in a collection

Posted: Tue Apr 08, 2014 5:54 pm
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?


How to generate a unique auto increment number in a collection

Posted: Tue Apr 08, 2014 6:22 pm
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.


How to generate a unique auto increment number in a collection

Posted: Sat Jul 05, 2014 11:51 am
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?


How to generate a unique auto increment number in a collection

Posted: Mon Jul 07, 2014 8:26 pm
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


How to generate a unique auto increment number in a collection

Posted: Sat Oct 11, 2014 7:47 pm
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...


How to generate a unique auto increment number in a collection

Posted: Mon Oct 13, 2014 8:55 pm
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).


How to generate a unique auto increment number in a collection

Posted: Wed Jul 22, 2015 4:34 am
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.


How to generate a unique auto increment number in a collection

Posted: Wed Jul 22, 2015 7:25 am
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


How to generate a unique auto increment number in a collection

Posted: Fri Nov 06, 2015 6:50 pm
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?


How to generate a unique auto increment number in a collection

Posted: Mon Nov 09, 2015 11:59 am
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).