Reputation: 16513
I am working on a vue.js project, this is how firebase handle my data now:
{
"books": {
"-Jldh2kvv9KyrkBuTTjV": {
"author": " Adam Freeman",
"title": "Pro AngularJS"
},
"-Jldh2kx0AgFXU1-Umnx": {
"author": "Niall OHiggins",
"title": "MongoDB and Python"
}
}
}
Can somebody help me to understand how I can set unique auto incremental ID like below:
{
"books": {
"001": {
"author": " Adam Freeman",
"title": "Pro AngularJS"
},
"002": {
"author": "Niall OHiggins",
"title": "MongoDB and Python"
}
}
}
Also, would be nice if you can explain what I am expecting to be done, is that good or bad way of managing data? I think it's not bad because it's my requirement.
Upvotes: 1
Views: 2393
Reputation: 900
When you use push()
method in firebase it will automatic generate unique key for your record. use set()
or update()
method to create your own custom keys.
you can use custom keys by generating timestamp
var bookCount = "bookCount _" + Math.round((new Date().getTime() / 1000));
Upvotes: 3
Reputation: 18670
Easiest way of handling list of data is by just using the default firebase database auto generated key's like your top example.
You can sort the key's which would sort the data from oldest to newest or vice versa.
I don't know why you want to add numeric id's to your data, but firebase's default unique id's are awesome.
Upvotes: 0