Reputation: 1601
Firestore documentation recommends checking document for existance. What is the case for document to not exist if it was queried from the database?
$citiesRef = $db->collection('cities');
$query = $citiesRef->where('capital', '=', true);
$documents = $query->documents();
foreach ($documents as $document) {
if ($document->exists()) {
printf('Document data for document %s:' . PHP_EOL, $document->id());
print_r($document->data());
printf(PHP_EOL);
} else {
printf('Document %s does not exist!' . PHP_EOL, $snapshot->id());
}
}
Upvotes: 1
Views: 164
Reputation: 3551
It's true that all documents returned by a query do exist. The checks for existence are useful when you get a document directly.
$collection->document('anything')
will always return a document reference, whether the document exists or not, so you can use exists()
to determine how you want to proceed with that document.
Upvotes: 3
Reputation: 1657
There is no case for a document not to exist if it appeared as the result of iterating over an existing snapshot (citing Creating Empty Documents in Firestore).
I suspect this is an oversight in the documentation. $db->collection('cities')
is an example of something that actually could be non-existent (in theory), although the example code does not check that.
Upvotes: 1