Python Driver: Document Collection Metadata

A server with Prism 2.2 document metadata support can describe collections through EntitiesRequest. This is the same Prism metadata request used for relational tables. A successful connection to a Prism 2.1 server establishes protocol compatibility for queries. Collection metadata additionally requires the 2.2 server implementation.

Returned metadata

Each document entity in an EntitiesResponse contains a Document message.

Field Meaning
namespace_name and collection_name Identify the collection.
fields Repeated DocumentField messages containing field_path, type_name and is_optional.
sample_document_json Optional JSON sample. Check presence with HasField("sample_document_json") before parsing it. Empty collections can omit it.
document_indexes Repeated DocumentIndex messages containing field_path, index_type and unique. An empty list means the server has no index metadata to report.

The server can infer fields from a bounded sample. Polypheny samples up to 100 documents per collection. is_optional indicates a field that is missing or null in some sampled documents or declared optional. A false value describes the observed sample. Types include string, int, double, bool, array, object, null and mixed. A field observed with both integer and floating-point values is reported as double.

Paths use dots between nested keys, for example address.city. A literal dot in a key is escaped as \. and a literal backslash as \\. Index paths follow the same convention. An index’s unique flag describes uniqueness declared by a single-field index for the documents the index covers. A field that is part of a compound unique index is reported with unique=false for that index entry. See Document metadata in the Prism 2.2 definitions for the field numbers and types.

Request metadata

Connector 2.0.0 has no public metadata method. The example uses the connector’s private connection.con and generated Prism messages, so pin connector 2.0.0 to use it as written. If you are implementing a Prism client, see Developing with Prism for the transport and request flow.

Run this example against a server with the MQL plugin enabled and a store that supports document collections. The account needs permission to create and drop a document namespace. The example creates a unique namespace, inserts a document and then drops the namespace.

import json
import os
from uuid import uuid4

import polypheny

connection = polypheny.connect(
    (os.getenv("POLYPHENY_HOST", "127.0.0.1"), int(os.getenv("POLYPHENY_PORT", "20590"))),
    username=os.getenv("POLYPHENY_USER", "pa"),
    password=os.getenv("POLYPHENY_PASSWORD", ""),
    transport="plain",
)
namespace = f"py_meta_{uuid4().hex[:12]}"
cursor = connection.cursor()
created = False
try:
    cursor.execute(f"CREATE DOCUMENT NAMESPACE {namespace}")
    created = True
    cursor.executeany("mongo", 'db.createCollection("patients")', namespace=namespace)
    patient = {"_id": "patient-1", "name": "Ada", "address": {"city": "Basel"}}
    cursor.executeany(
        "mongo", f"db.patients.insertOne({json.dumps(patient)})", namespace=namespace
    )
    connection.commit()

    request = connection.con.new_request()
    request.entities_request.namespace_name = namespace
    request.entities_request.entity_pattern = "patients"
    response = connection.con.call(request).entities_response
    document = next(entity.document for entity in response.entities if entity.HasField("document"))
    print(document.namespace_name, document.collection_name)
    for field in document.fields:
        print(field.field_path, field.type_name, field.is_optional)
    if document.HasField("sample_document_json"):
        print(json.loads(document.sample_document_json))
    for index in document.document_indexes:
        print(index.field_path, index.index_type, index.unique)
finally:
    try:
        connection.rollback()
        if created:
            cursor.execute(f"DROP NAMESPACE {namespace}")
    finally:
        connection.close()
© Polypheny GmbH. All Rights Reserved.