繁体   English   中英

Firestore如何获取集合的最后一个文档并添加具有递增ID的新文档?

[英]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/

这个怎么运作:

  • 我向事件集合内部的每个虚拟事件添加了唯一的eventId,
  • 根据该唯一的eventId,我创建指向特定单个Event.js的链接,
  • 用户可以在/ create-event中创建提供信息的事件,
  • 一旦有人创建了事件,我想将事件添加到具有增量ID的事件集合中,那么我已经在控制台内部添加了7个事件,因此下一个应该具有id = 7,可能类似于event1,event2 ...具有autoincrement
  • 在用户集合内部,我存储来自用户提供的身份验证和主机名的currentUser.uid
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
    });


Firestore设置

谢谢

我了解您希望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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM