-2
import firebase from "../firebase";
import React, { useState, useEffect } from "react";
import { useLocation } from "react-router-dom";

const Result = (props) => {
  const { state } = useLocation();
  const { exam, category, councelling, branch, gender, rank } = state.state;
  const [result, setResult] = useState([]);

  const db = firebase.firestore();
  const que = category + "_" + gender + "_CR";
  const docref = db
    .collection(exam)
    .doc("2019-2020")
    .collection("RankAnalysis")
    .where(
      (firebase.firestore.FieldPath.documentId()
        .toString()
        .substring(
          firebase.firestore.FieldPath.documentId().toString().length -
            branch.length
        ),
      "==",
      branch)
    )
    .where(rank, "<", que);

  useEffect(() => {
    const fetchData = async () => {
      docref
        .get()
        .then((querySelector) => {
          querySelector.forEach((doc) => {
            console.log(doc.data());
          });
        })
        .catch((error) => {
          console.log("Error getting documents: ", error);
        });
    };

    fetchData();
  }, [docref]);

  return (
    <div>
      <h1>{exam}</h1>
      <h1>{branch}</h1>
      <h1>{category}</h1>
      <h1>{gender}</h1>
      <h1>{councelling}</h1>
      <h1>{rank}</h1>
    </div>
  );
};

export default Result;

The point is that I want to get the documents from firestore which specifically contains a substring (i.e., branch) at the end, so I used fieldpath.documentId() which returns a fieldpath(I converted it into a string), But Nothing seems to be worked out. the database is structured like:-

see here

The document name be like AAAACSE i want to get the documents which contains a substring "CSE" (dynamic).

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Anirudh Saladi
  • 87
  • 1
  • 2
  • 9

1 Answers1

0

Firestore cannot filter for IDs/values that end with a certain substring. The only string criteria it can handle is "is equal" (through ==) and "starts with" (through >= and <=).

So the only way to implement your use-case in Firestore would be to revere the IDs and filter on that.

If you need stronger text filtering capabilities, consider using another/additional solution for that - such as shown in the Firebase documentation on adding full-text search to Firestore.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807