简体   繁体   中英

React Native Firebase How To Have Auto Document Id Increment by 1

So I would simply like to have auto increment. Currently whatever I create in my app that document in firebase gets automatic big random id. I would like for each new document inside collection to simply have 1+ id, for example I mean first document inside collection gets id = 1, second document will automatically get id=2 and so on... here is my code of for example creating a post:

addPost = async ({ text, avatar, name }) => {

    return new Promise((res, rej) => {
      this.firestore
        .collection("posts")
        .add({
          text,
          //text2,
          //id,
          uid: this.uid,
          timestamp: this.timestamp,
          avatar,
          name,
        })
        .then((ref) => {
          ref.update({ id: ref.id });
          res(ref);
        })
        .catch((error) => {
          rej(error);
        });
    });
  };

I can at least partially answer this - a monotonically changing ID (ie incrementing) is HIGHLY DISCOURAGED in Firestore. The random distribution of those "large ids" is a fundamental performance feature of firestore at scale. If you need some sort of order, use a field in the document, not the documentID.

HOW I SOLVED THIS: I CREATED A NEW COLLECTION CALLED "id" and gave it only one field called also "id" and of type number. I put value of 0. The secret is to get this value in a state, and increment it every time I make a new post(aka every time I want to icnrement my id in some other collection). Then for the id I want incremented in my other collection i use that state and put +1. Complicated, but for anybody who needs it here is the code:

addPost = async ({ text, avatar, name }) => {
    return new Promise((res, rej) => {
      this.provjera();

      this.firestore
        .collection("posts")
        .add({
          text,
          id: this.state.specialid + 1,
          uid: this.uid,
          timestamp: this.timestamp,
          avatar,
          name,
        })
        .then((ref) => {
          
          console.log(this.state.specialid);
          this.setState({ specialid: this.state.specialid + 1 });
          this.firestore
            .collection("id")
            .doc("hl6ucFvzQ18lIFae9feo")
            .update({ id: this.state.specialid });
          //console.log(this.state.specialid);
          res(ref);
        })
        .catch((error) => {
          rej(error);
        });
    });
  };

and here is check method:

check = async () => {
    const yolo = [];
    const snapshot = await firebase
      .firestore()
      .collection("id")
      .get()
      .then((snapshot) => {
        snapshot.docs.forEach((doc) => {
          const { id } = doc.data();
          yolo.push({ id: id });
          this.setState({ specialid: yolo[0].id });  //this gets field value of id inside collection id which is 0.
        });
      });
  };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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