Professional Cloud Database Engineer free question 3 / 10 · Domain 1: Design innovative, scalable, and highly available cloud database solutions
A Firestore collection holds documents that have a rank field and documents in which the rank field is missing. What does a query with orderBy(rank) return?
- Documents in which rank is missing are ordered the same way as documents with a null value
- Documents in which rank is missing are ordered as if rank were 0
- Documents in which rank is missing are not included in the orderBy results
- Documents in which rank is missing are placed first, in document ID order
Show the answer and explanation
Answer: C
Why this is correct
A note on the order and limit data page says that an orderBy() clause also filters: the query results do not include documents that lack the field you order by. A document where the field holds null does have the field, so it is returned in the ordering, while a document with no rank field at all drops out of the results. To order the full set, give every document the field you sort on.
Why the other options are wrong
"Documents in which rank is missing are ordered the same way as documents with a null value" … A missing field and a field that exists with a value of null are not the same state.
"Documents in which rank is missing are ordered as if rank were 0" … The missing field is not filled in with the number 0. It is distinct from a document that explicitly holds 0.
"Documents in which rank is missing are placed first, in document ID order" … Missing documents are not gathered at the front. They are excluded from the query.
Key point
If you need the complete set ordered, prepare a consistent sort field on the documents that need it. Compare document counts before and after any data repair.