Kimsora✨
article thumbnail
Published 2023. 9. 10. 16:28
파이어베이스 파헤치기-2 기타
320x100
반응형

실시간 업데이트 

-onSnapShot:문서 스냅샷을 즉시 생성하고 내용이 변경될 때마다 콜백이  호출되어 문서 스냅샷을 업데이트 한다

   const docRef=collection(dbService, "text"),
 
	 onSnapshot(docRef, (snap) => {
      const snapArr = snap.docs.map((doc) => ({ id: doc.id, ...doc.data() }));
      setTextList(snapArr);
    });

 

쿼리문

단순쿼리:단일 필드를 기준으로 데이터를 필터링 하고 정렬할때 사용된다 where,orderBy조건을 사용한다

where:특정 필드 값응ㄹ 기준으로 데이터를 필터링한다

// "doc" 컬렉션에서 "text" 필드가 "baba"인 문서를 필터링
db.collection("doc")
  .where("text", "==", "baba")
  .get()
  .then((querySnapshot) => {
    querySnapshot.forEach((doc) => {
      // 검색된 문서를 처리
      const textData = doc.data();
      console.log("text info:", textData.info);
    });
  })
  .catch((error) => {
    console.log("Error getting documents:", error);
  });

 

orderBy:검색 결과를 특정 필드를 기준으로 정렬 가능하다

// "fruits" 컬렉션에서 "rank" 필드를 기준으로 내림차순으로 정렬하여 검색
db.collection("fruits")
  .orderBy("rank", "desc")
  .get()
  .then((querySnapshot) => {
    querySnapshot.forEach((doc) => {
      // 정렬된 문서를 처리
      const fruitsList= doc.data();
      console.log("fruits Name:", fruitsList.name, "Population:", fruitsRank.rank);
    });
  })
  .catch((error) => {
    console.log("Error getting documents:", error);
  });

 

복합쿼리:여러 조건을 결합하여 데이터를 필터리하고 검색하여 사용한다 where 조건을 여러번 사용하여 조건을 결합한다

// "cities" 컬렉션에서 "state"가 "California"이고 "population"이 100,000 이상인 문서를 필터링
db.collection("cities")
  .where("state", "==", "California")
  .where("population", ">=", 100000)
  .get()
  .then((querySnapshot) => {
    querySnapshot.forEach((doc) => {
      // 검색된 문서를 처
      const cityData = doc.data();
      console.log("City Name:", cityData.name, "Population:", cityData.population);
    });
  })
  .catch((error) => {
    console.log("Error getting documents:", error);
  });

 

 

💡where 메서드 연산자

  1. <(미만):필드 값이 지정한 값보다 작은 문서를 필터링 
  2. <=(작거나 같음):필드 값이 지정한 값보다 작거나 같은 움서 필터링
  3. ==(같음):필드 값이 지정한 값과 정확히 일치하는 문서 필터링
  4. >(초과):필드 값이 지정한 값보다 큰 문서를 필터링
  5. >=(이상):필드 값이 지정한 값보다 크거나 같은 문서 필터링
  6. !=(같지 않음):필드 값이 지정한 값과 정확히 일치하지 않는 문서 필터링
  7. array-contains:배열 필드에 지정한 값이 포함된 문서를 필터링
// "cities" 컬렉션에서 "features" 배열 필드에 "ocean"을 포함하는 문서를 필터링
db.collection("cities")
  .where("features", "array-contains", "ocean")
  .get()
  .then((querySnapshot) => {
    querySnapshot.forEach((doc) => {
      // 검색된 문서를 처리할 수 있습니다.
      const cityData = doc.data();
      console.log("City Name:", cityData.name);
    });
  })
  .catch((error) => {
    console.log("Error getting documents:", error);
  });

  8.array-contains-any:배열 필드에 지정한 값중 하나라도 포함된 문서를 필터링

// "cities" 컬렉션에서 "features" 배열 필드에 "ocean" 또는 "mountain" 중 하나 이상을 포함하는 문서를 필터링
db.collection("cities")
  .where("features", "array-contains-any", ["ocean", "mountain"])
  .get()
  .then((querySnapshot) => {
    querySnapshot.forEach((doc) => {
      // 검색된 문서를 처리할 수 있습니다.
      const cityData = doc.data();
      console.log("City Name:", cityData.name);
    });
  })
  .catch((error) => {
    console.log("Error getting documents:", error);
  });

  9.in:필드 값에 지정한 여러 값중 하나와 일치하는 문서를 필터링

// "cities" 컬렉션에서 "country" 필드가 "USA", "Canada", 또는 "Mexico" 중 하나와 일치하는 문서를 필터링
db.collection("cities")
  .where("country", "in", ["USA", "Canada", "Mexico"])
  .get()
  .then((querySnapshot) => {
    querySnapshot.forEach((doc) => {
      // 검색된 문서를 처리할 수 있습니다.
      const cityData = doc.data();
      console.log("City Name:", cityData.name);
    });
  })
  .catch((error) => {
    console.log("Error getting documents:", error);
  });


  10.not-in:필드 값에 지정한 여러 값중 어느 하나와 일치하지 않는 문서를 필터링

// "cities" 컬렉션에서 "category" 필드가 "tourist", "historic", 또는 "cultural" 중 어느 하나와 일치하지 않는 문서를 필터링
db.collection("cities")
  .where("category", "not-in", ["tourist", "historic", "cultural"])
  .get()
  .then((querySnapshot) => {
    querySnapshot.forEach((doc) => {
      // 검색된 문서를 처리할 수 있습니다.
      const cityData = doc.data();
      console.log("City Name:", cityData.name);
    });
  })
  .catch((error) => {
    console.log("Error getting documents:", error);
  });
728x90
반응형

'기타' 카테고리의 다른 글

manifest.json 이란  (4) 2023.11.03
파이어베이스 파헤치기-1  (5) 2023.09.02
TDD  (0) 2022.12.02
figma  (0) 2022.10.26
profile

Kimsora✨

@sorarar

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!

검색 태그

WH