Auto increment field

Question:
[How can I make the ID field as auto increment when each new user submit the registration form? .]

Product:
Wix Editor

The _id field is randomly generated every time a new item is inserted into a collection. However you can also manually specify your own _id on insert when using Velo and wix-data.

The basic way is something like:

  • Do a query and sort data descending on _id: https://www.wix.com/velo/reference/wix-data/wixdataquery/descending
  • Make sure to use the consistentRead and omitTotalCount options on the find() call to get consistent reads and a bit more performance
  • limit(1) to only get the top most item
  • Get the _id of that item, increment by 1, and use that for the new item

May also want to consider indexes to speed up these types of operations: CMS: Adding an Index for Your Collection | Help Center | Wix.com

Also may want to consider handling conflicts (ie. what if 2 writes are happening at the same time and trying to use the same id?).

1 Like