admin管理员组文章数量:1426100
I want to store the user information in the document with its id(not the document id). I want to generate the id particular formate like XYZ0001, where the last 4 digits will automatically increase whenever a new user adds on. Eg, XYZ0001, XYZ0002 like that. I have tried this...
return firestore
.collection("xyzData")
.doc(resp.user.uid)
.set({
firstName: newUser.firstName,
lastName: newUser.lastName,
email: newUser.email,
ca_id: "XYZ" + pad(firestore.collection("xyzData").count())
})
.then
where pad() is a function which gives me last 4 digits
function pad(n) {
var s = "000" + n;
return s.substr(s.length - 4);
}
but this is giving me an error that count() is not a function
Also, I was thinking of one another way where I can read the value of the last id and then increase it by one for the next user, bust won't be able to implement it.
I want to store the user information in the document with its id(not the document id). I want to generate the id particular formate like XYZ0001, where the last 4 digits will automatically increase whenever a new user adds on. Eg, XYZ0001, XYZ0002 like that. I have tried this...
return firestore
.collection("xyzData")
.doc(resp.user.uid)
.set({
firstName: newUser.firstName,
lastName: newUser.lastName,
email: newUser.email,
ca_id: "XYZ" + pad(firestore.collection("xyzData").count())
})
.then
where pad() is a function which gives me last 4 digits
function pad(n) {
var s = "000" + n;
return s.substr(s.length - 4);
}
but this is giving me an error that count() is not a function
Also, I was thinking of one another way where I can read the value of the last id and then increase it by one for the next user, bust won't be able to implement it.
Share Improve this question edited Mar 27, 2023 at 21:10 Renaud Tarnec 83.3k10 gold badges98 silver badges129 bronze badges asked Dec 9, 2019 at 11:09 SIDDHARTH VARSHNEYSIDDHARTH VARSHNEY 6591 gold badge8 silver badges17 bronze badges1 Answer
Reset to default 8Update on fall 2022
At the Firebase Summit 2022, Firebase announced that now Cloud Firestore supports the count()
aggregation query. count()
allows you to determine the number of documents in a collection or query.
Old answer, prior to fall 2022
Note that there is no count()
method for a CollectionReference
. If you want to know the number of documents in a collection you need to call the asynchronous get()
method to get a QuerySnapshot
and then use the size
property. However, you can not pass a call to get()
as a property of an object.
The solution is to maintain you own counter.
There are two standard ways for that:
Use firebase.firestore.FieldValue.increment()
As explained in the doc you can call firebase.firestore.FieldValue.increment()
to increment a specific field of a Firestore document. For example, you could have one document in a counter
Collection that you increment and read each time you want to get a new value from the sequence. Depending on your exact use case, you may need to use a Transaction
.
Also note that "you can update a single document only once per second".
Distributed counter
If you need to update your counter document more than once per second, you should use a Distributed counter as explained in the doc.
本文标签: javascriptUsing a number sequence to define Firestore document field valueStack Overflow
版权声明:本文标题:javascript - Using a number sequence to define Firestore document field value - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745469457a2659683.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论