[英]Firestore how to get last document of collection and add new with incremented id?
我的事件集合中的文档的自动生成ID可能出错了。 我为生成的事件添加了eventId,并手动将eventId分配给每个事件。
我能以某种方式获取带有其eventId的最后一个文档,并添加带有最后一个文档的eventId递增一个的新文档。
还是应该删除基于自动生成的ID的事件,并使用非自动生成的ID创建新的事件?
GitHub: https : //github.com/Verthon/event-app
我已经在netlify上发布了可运行的React.js应用程序: https ://eventooo.netlify.com/
这个怎么运作:
Event Creator
submitEvent = (e) => {
e.preventDefault();
const eventRef = this.props.firebase.db.collection("events").doc();
eventRef.set({
title: this.state.title,
host: this.state.host,
localization: this.state.localization,
description: this.state.description,
category: this.state.category,
day: this.state.day,
hour: this.state.hour,
featuredImage: this.state.imageUrl,
uid: this.props.firebase.auth.currentUser.uid
});
const citiesRef = this.props.firebase.db.collection("cities").where("city", "==", this.state.localization);
const cityRef = this.props.firebase.db.collection("cities").doc();
citiesRef.get()
.then(querySnapshot => {
if(querySnapshot.exists){
console.log("exist");
return
}else{
cityRef.set({
city: this.state.localization,
})
}
});
const userRef = this.props.firebase.db.collection("users").doc();
userRef.set({
user: this.state.host,
uid: this.props.firebase.auth.currentUser.uid
});
谢谢
我了解您希望eventId
值来自数字序列。 满足此需求的最佳方法是使用分布式计数器,如文档中所述: https : //firebase.google.com/docs/firestore/solutions/counters
我不知道您使用的是哪种语言,但是我将本文档中这三个函数的JavaScript代码粘贴到下面,并编写了将生成可用于创建文档的序列号的代码。
var db = firebase.firestore();
//From the Firebase documentation
function createCounter(ref, num_shards) {
var batch = db.batch();
// Initialize the counter document
batch.set(ref, { num_shards: num_shards });
// Initialize each shard with count=0
for (let i = 0; i < num_shards; i++) {
let shardRef = ref.collection('shards').doc(i.toString());
batch.set(shardRef, { count: 0 });
}
// Commit the write batch
return batch.commit();
}
function incrementCounter(db, ref, num_shards) {
// Select a shard of the counter at random
const shard_id = Math.floor(Math.random() * num_shards).toString();
const shard_ref = ref.collection('shards').doc(shard_id);
// Update count
return shard_ref.update(
'count',
firebase.firestore.FieldValue.increment(1)
);
}
function getCount(ref) {
// Sum the count of each shard in the subcollection
return ref
.collection('shards')
.get()
.then(snapshot => {
let total_count = 0;
snapshot.forEach(doc => {
total_count += doc.data().count;
});
return total_count;
});
}
//Your code
var ref = firebase
.firestore()
.collection('counters')
.doc('1');
var num_shards = 2 //Adapt as required, read the doc
//Initialize the counter bay calling ONCE the createCounter() method
createCounter(ref, num_shards);
//Then, when you want to create a new number and a new doc you do
incrementCounter(db, ref, num_shards)
.then(() => {
return getCount(ref);
})
.then(count => {
console.log(count);
//Here you get the new number form the sequence
//And you use it to create a doc
db.collection("events").doc(count.toString()).set({
category: "education",
//....
})
});
在功能需求上没有太多细节的情况下,很难说将序列中的数字用作文档的uid或用作文档中的字段值之间是否存在差异。 这取决于您可能对此集合执行的查询。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.