DEV Community

Cover image for  "array-contains-any" & "IN query" now on Firebase Firestore! Sample codes and demo.
nabettu
nabettu

Posted on • Updated on

"array-contains-any" & "IN query" now on Firebase Firestore! Sample codes and demo.

The convenient query array-contains-any & IN query has finally entered the JS SDK, so I tried using it!

Since it has just entered the SDK, there is no shadow or shape in the official document yet (as of 10/30/2019)

What is array-contains-any

Until now, if you specified "array-contains" for "where", you could search for a document with a specific value in array.

The actual code looks like this, and you can search for documents with "tech" in the array field in the blogs collection.

firebase
      .firestore()
      .collection("blogs")
      .where("tags", "array-contains", "tech")
      .get()

array-contains-any is an evolution of it. You can search for documents where the array contains one of the specific values.

The actual code looks like this, and you can search for documents that contain either "tech" or "game" in the array field in the blogs collection.

Basically the same as array-contains, but you can pass an array as the third argument of where.

firebase
      .firestore()
      .collection("blogs")
      .where("tags", "array-contains-any", ["tech", "game"] )
      .get()

And if any of this array is in, it will hit the search.

Now you can make a query that can describe an OR condition. Therefore, I think that the range that can be searched with Firestore alone will expand.

What is IN Query

The usage is as follows!

firebase
      .firestore()
      .collection("blogs")
      .where("type", "in", ["tweet", "podcast"] )
      .get()

A query that hits if it exactly matches either tweet or podcast in the type field.

You can now OR search for specific fields like array-contains-any! (Since it is an exact match, free string search is not possible yet)

Example of use) In a fold that contains a fixed type as described above, you can search for "when it matches a tweet or podcast".

This is a query that executes the data corresponding to "==" in all the arrays and summarizes the results.

Created a sample site

https://firestore-sandbox.netlify.com

Here you can execute both Array Contains and Array Contains Any on the sample data.

Click here for the repository.

https://github.com/nabettu/firestore-sandbox

Summary

The conditions that can be used so far are the same as array-contains, but the range of search can be made without using Algolia, so it seems to be useful!

that's all!

Top comments (6)

Collapse
 
saslamp profile image
Abubakar Sambo

Hi,
In my case I'm searching for multiple, can't figure out how to go about it.

Like;

firebase
.firestore()
.collection("blogs")
.where("type, type2", "in", ["tweet", "podcast"] )
.get()

I've tried 'array-contains-any'. Doesn't seem to be the answer. Any help please

Collapse
 
nabettu profile image
nabettu

You can not use any where-in in one query.

You can use like query.

firebase
.firestore()
.collection("blogs")
.where("type", "==", "podcast" )
.where("tags", "in", ["tweet", "podcast"] )
.get()

sorry maybe cannot search multiple field.

Collapse
 
phillipmalboeuf profile image
Phillip Malboeuf

in is also in for those interested!

Collapse
 
nabettu profile image
nabettu

thx comment!
I added about IN Query!

Collapse
 
caed_x profile image
Alejandro

What sdk version does it support? with 5.9.1 doesn't recognize me

Collapse
 
nabettu profile image
nabettu

Version 7.3.0!