0

I'm using multiselection in one of my parameters and I would like to know how to query those parameters like for example if

I want to query parameters that has 1 (doesn't matter if there are other values)

Value one has : 1, 2, 3 Value two has: 5, 1, 6 Value three has: 5, 6, 9

It should only bring Value one and two

I know you can do something like (for non array values):

const librosRef = db.collection('libros');
const queryRef = librosRef.where('grado', '==', '4° Grado');

and it would bring all the documents in that collection that has 4° Grado but if I try to do that while using a multiselection it doesn't bring anything.

This is what I'm trying (doesn't work for array which is what I'm trying to figure out):

const productosRef = db.collection('productosAIB');
    const queryRef = productosRef.where('grado', '==', '4° Grado');

    useEffect(() => {
        queryRef.orderBy("precio")
        .get()
        .then((snapshot) => {
              const tempData = [];
            snapshot.forEach((doc) => {

              const data = doc.data();
              tempData.push(data);
            });
            setProductos(tempData);
          });
      }, []);

Example of how it gets stored in the Firebase:

enter image description here

And this is how it looks in the table (without the query because if I add the query it doesn't show anything )

enter image description here

2
  • Please show an example of your Firestore document Commented Sep 20, 2021 at 21:29
  • Oh I forgot about it, I just added it. Commented Sep 20, 2021 at 21:36

1 Answer 1

1

It sounds like you're trying to query for documents based on the existence of a value in an array. These are called array membership queries in Firestore.

For this, you would use array-contains to match a single field in an array, and array-contains-any to match any value from an array.

To query based on single value in array

const queryRef = productosRef.where('grado', 'array-contains', '4° Grado');

multiple values passed in as an array

const queryRef = productosRef.where('grado', 'array-contains-any', ['4° Grado', 'next array element']);

NOTE: array-contains-any can support up to 10 comparison values.

For more information on array membership queries you can see the documentation here

Sign up to request clarification or add additional context in comments.

6 Comments

I'll check the documentation, Edit: for some reason it doesn't show up anything in the 4th grade even though I have 2 values, 1 alone and one with multiple values.
Found my issue apparently I'm not suppose to try and order. As soon as I removed the .orderBy("precio") it worked perfectly, thank you now I have to figure it out why I can't order by price lol
@ReactPotato can you show your query and also try logging out the data to the console
My query: const queryRef = productosRef.where('grado', 'array-contains-any',['4° Grado']);
My code in the use effect: queryRef.get() This works | queryRef.orderBy("precio").get() Doesn't work but doesn't crash | queryRef.get().orderBy("precio") doesn't work and crash
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.