diff --git a/firestore/google/cloud/firestore_v1/field_path.py b/firestore/google/cloud/firestore_v1/field_path.py index 7552f2ec145b..58b4f3b9acd3 100644 --- a/firestore/google/cloud/firestore_v1/field_path.py +++ b/firestore/google/cloud/firestore_v1/field_path.py @@ -384,3 +384,12 @@ def lineage(self): """ indexes = six.moves.range(1, len(self.parts)) return {FieldPath(*self.parts[:index]) for index in indexes} + + @staticmethod + def document_id(): + """A special FieldPath value to refer to the ID of a document. It can be used + in queries to sort or filter by the document ID. + + Returns: A special sentinel value to refer to the ID of a document. + """ + return "__name__" diff --git a/firestore/tests/system.py b/firestore/tests/system.py index a8e683629add..b5a66665158f 100644 --- a/firestore/tests/system.py +++ b/firestore/tests/system.py @@ -729,8 +729,12 @@ def test_collection_group_queries_filters(client, cleanup): query = ( client.collection_group(collection_group) - .where("__name__", ">=", client.document("a/b")) - .where("__name__", "<=", client.document("a/b0")) + .where( + firestore.field_path.FieldPath.document_id(), ">=", client.document("a/b") + ) + .where( + firestore.field_path.FieldPath.document_id(), "<=", client.document("a/b0") + ) ) snapshots = list(query.stream()) found = set(snapshot.id for snapshot in snapshots) @@ -738,9 +742,13 @@ def test_collection_group_queries_filters(client, cleanup): query = ( client.collection_group(collection_group) - .where("__name__", ">", client.document("a/b")) .where( - "__name__", "<", client.document("a/b/{}/cg-doc3".format(collection_group)) + firestore.field_path.FieldPath.document_id(), ">", client.document("a/b") + ) + .where( + firestore.field_path.FieldPath.document_id(), + "<", + client.document("a/b/{}/cg-doc3".format(collection_group)), ) ) snapshots = list(query.stream()) diff --git a/firestore/tests/unit/v1/test_field_path.py b/firestore/tests/unit/v1/test_field_path.py index 5221321ad10c..55aefab4c152 100644 --- a/firestore/tests/unit/v1/test_field_path.py +++ b/firestore/tests/unit/v1/test_field_path.py @@ -493,3 +493,8 @@ def test_lineage_nested(self): field_path = self._make_one("a", "b", "c") expected = set([self._make_one("a"), self._make_one("a", "b")]) self.assertEqual(field_path.lineage(), expected) + + def test_document_id(self): + parts = "__name__" + field_path = self._make_one(parts) + self.assertEqual(field_path.document_id(), parts)