待ちに待ったFirestoreにARRAY_CONTAINS_ANYとIN検索がキタ!!

kooooohe
kooooohe
Nov 8 · 4 min read

v2.5.0よりの新機能

Features
・ introduces ARRAY_CONTAINS_ANY and IN to operator enum (2c8869d)

何が革命的か??

例えば、自分が今持っている材料を条件に、料理一覧から対象の料理を持ってくるシステムがあるとする。

今まで

「パターン1」: クエリを複数回投げ、プログラムで重複を後で削除する

db.collection("FoodList").where("材料", "array-contains", "玉ねぎ").get()
db.collection("FoodList").where("材料", "array-contains", "人参").get()

「パターン2」: すべて取得し、プログラムで対象外のデータを削除する

db.collection("FoodList").get()

「パターン3」Algoliaなどのサードパーティーサービスを使う

これまでであれば、このパターンが一番多かったであろう。

これからの話しをしよう

array-contains-any を使い1発で取得する

db.collection("FoodList").where("材料", "array-contains-any", ["人参", "玉ねぎ"]).get()

anti-pattern-engineering

This publication is a tech-blog by Anti-Pattern Inc.(https://anti-pattern.co.jp/)

kooooohe

Written by

kooooohe

I like Zero to One.

anti-pattern-engineering

This publication is a tech-blog by Anti-Pattern Inc.(https://anti-pattern.co.jp/)

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade