skip to Main Content

I’m trying to make a Firestore query to get all my incoming meetings. Some meetings have a date, and some don’t (they will be scheduled later). Here’s my current query:

query(
  collection,
  orderBy('date'),
  or(where('date', '==', null), where('date', '>=', new Date()))
);

However, when I run this query, I get the error:

Order by clause cannot contain a field with an equality filter date

I understand this error in certain cases, but I have an OR condition that is supposed to handle both cases.

To provide some additional context, I’m using Cloud Firestore and a front-end client to query the database. I’ve tried using different approaches and queries, but I haven’t been able to find a solution.

I’ve also checked the Firestore documentation and other resources for help, but I suspect this may be a bug.

If anyone has any ideas on how to order the results by date while still including meetings without a date, I would greatly appreciate it.
Thank you.

2

Answers


  1. Why Dont you get the two seperate like

    const getScheduledWithoutDate = query(
      collection,
      where('date', '==', null),
    );
    
    const getScheduledWithDate = query(
      collection,
      where('date', '!==', null), 
      where('date', '>=', new Date()))
    );
    

    mind the syntax is not correct im on typescript just want to get the point in
    then sort them locally

    Login or Signup to reply.
  2. From the list of query limitations in the Firestore documentation:

    You can’t order your query by a field included in an equality (==) or in clause.

    I’m actually not sure why this limitation exists, so I’m gonna ask around – but it doesn’t seem to be a bug as you expect.


    Would it be possible to rewrite the query to use != instead of ==?

    query(
      collection,
      orderBy('date'),
      where('date', '!=', null), 
      where('date', '>=', new Date())
    );
    

    And in fact, it might already work with just that last condition as null is not an actual date value:

    query(
      collection,
      orderBy('date'),
      where('date', '>=', new Date())
    );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search