D
D
dfhkjhg2020-08-27 22:20:55
JavaScript
dfhkjhg, 2020-08-27 22:20:55

How to assign a unique id for each purchased item?

Let's say there is a store where a user buys an item:

{
 id: 25,
 added_at: 1598555928476
}

Items are stored in users' inventories but they are often removed from the inventory or new items added

Is it possible to make each new item get id === lastid + 1 without saving purchased items in a separate table?
Or what is the best way to do this?)
(I hope I explained it clearly)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander, 2020-08-27
@Alexandre888

var id = 0;
var obj1 = {
  id: ++id
}

var obj2 = {
 id: ++id
}
// . . . //
console.log(obj1) // 1
console.log(obj2) // 2

H
hzzzzl, 2020-08-27
@hzzzzl

type analogue of sql auto increment for mongo?
there are plugins for mongoose like this
https://github.com/chevtek/mongoose-auto-increment
but in general how would it be better to use the built-in unique _id

K
Karpion, 2020-08-28
@Karpion

What for? And what if a person bought two identical items?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question