2

How to return all documents having specific field as null when we apply orderBy on that specific field in collection in firestore

For example I have a 2 document one having "createdDate" field and other not. I make a query to orderMy Date acc to "createdDate field". firestore.collection("collection").orderBy("createdDate").get()

The result I am getting has only 1 record, that record contains createdDate and It skipped that document that doesn't have "createdDate" field

But I want all the documents

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Geetanshu Gulati
  • 692
  • 8
  • 13
  • Hi Geetanshu, you may want to complete the queston with code sample, this could help answers. Also, this is probably a duplicate of https://stackoverflow.com/questions/48479532/firestore-select-where-is-not-null – Hugo Gresse Aug 07 '20 at 12:43
  • Does this answer your question? [Firestore select where is not null](https://stackoverflow.com/questions/48479532/firestore-select-where-is-not-null) – Hugo Gresse Aug 07 '20 at 12:43
  • I have explained It more, Is it okay now ? – Geetanshu Gulati Aug 07 '20 at 13:15
  • 1
    Hmm. You're asking to order a collection of documents by a field that may not exist? What would the expected result be? That's kind of a design flaw as well which may lead to other trouble down the road. In other words, if I wanted to get a list of user names from a users collection, if I order by their name, what would the expected name be for the users that don't have that field at all? Also, what if I wanted to also get a count of users that actually have a name field? If the ones that don't are returned, the count would be wrong. Better to ensure the name field exists with a null value. – Jay Aug 07 '20 at 15:34

1 Answers1

2

[Firebaser here] this is an intrinsic limitation of Firestore, when you orderBy a field you filter out all documents that do not have that field.

There is no way to query documents which do not have a certain field in Firestore.

That said there is a difference between no value and null value. If you store null in the field by default, you can query for that.

Sam Stern
  • 24,624
  • 13
  • 93
  • 124