FG
๐Ÿ”Œ APIs & SDKsGoogle

FR: Firestore OR operator in WHERE query

Freshabout 20 hours ago
Mar 14, 20260 views
Confidence Score95%
95%

Problem

[REQUIRED] Describe your environment Operating System version: N/A Firebase SDK version: 4.6.2 * Firebase Product: firestore [REQUIRED] Describe the problem Rather than having to make several separate queries I would like to see an OR operator for the WHERE query in Firestore. This is something that is currently available in MongoDB or with the $or operator here. Relevant Code: The query could look something like this: [code block] or better yet [code block]

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix โ€“ Awaiting Verification

Implement Firestore OR Queries Using Array-Contains

Medium Risk

Firestore does not support the OR operator directly in queries. Instead, it requires separate queries for each condition. This limitation arises from Firestore's underlying architecture, which is optimized for scalability and performance, but restricts complex query operations like OR.

Awaiting Verification

Be the first to verify this fix

  1. 1

    Refactor Query Using Array-Contains

    To simulate an OR operation, you can use the array-contains operator to filter documents that match any of the specified values. This requires restructuring your data slightly to include an array field that holds the values you want to query against.

    typescript
    const query = firestore.collection('yourCollection').where('yourField', 'array-contains', 'value1');
  2. 2

    Modify Data Structure

    Ensure that the documents in your collection have an array field that includes all potential values you want to query. For example, if you want to check for 'value1' or 'value2', your documents should have an array field like this: { yourField: ['value1', 'value2'] }.

    typescript
    const doc = { yourField: ['value1', 'value2'] }; firestore.collection('yourCollection').add(doc);
  3. 3

    Perform Multiple Queries for Complex Conditions

    If you need to check multiple fields or conditions, perform separate queries for each condition and merge the results in your application logic. This is a workaround to achieve the desired outcome.

    typescript
    const query1 = firestore.collection('yourCollection').where('yourField', '==', 'value1');
    const query2 = firestore.collection('yourCollection').where('yourField', '==', 'value2');
    const results1 = await query1.get();
    const results2 = await query2.get();
    const combinedResults = [...results1.docs, ...results2.docs];
  4. 4

    Test the Implementation

    Run your application and ensure that the queries return the expected results. Check that documents matching either condition are retrieved correctly.

    typescript
    console.log(combinedResults);

Validation

Confirm that the returned documents include all expected matches for the OR conditions. Compare the results with the expected output to ensure correctness.

Sign in to verify this fix

Environment

Submitted by

AC

Alex Chen

2450 rep

Tags

firebasegooglesdkapi:-firestore