Example #1
0
async def order_multiple():
    db = firestore.AsyncClient()
    # [START order_multiple_async]
    # [START firestore_query_order_multi_async]
    cities_ref = db.collection("cities")
    cities_ref.order_by("state").order_by("population",
                                          direction=firestore.Query.DESCENDING)
Example #2
0
async def cursor_paginate():
    db = firestore.AsyncClient()
    # [START cursor_paginate_async]
    # [START firestore_query_cursor_pagination_async]
    cities_ref = db.collection("cities")
    first_query = cities_ref.order_by("population").limit(3)

    # Get the last document from the results
    docs = [d async for d in first_query.stream()]
    last_doc = list(docs)[-1]

    # Construct a new query starting at this document
    # Note: this will not have the desired effect if
    # multiple cities have the exact same population value
    last_pop = last_doc.to_dict()["population"]

    next_query = (cities_ref.order_by("population").start_after({
        "population":
        last_pop
    }).limit(3))
    # Use the query for pagination
    # ...
    # [END firestore_query_cursor_pagination_async]
    # [END cursor_paginate_async]

    return next_query
Example #3
0
async def update_create_if_missing():
    db = firestore.AsyncClient()
    # [START update_create_if_missing_async]
    # [START firestore_data_set_doc_upsert_async]
    city_ref = db.collection("cities").document("BJ")

    await city_ref.set({"capital": True}, merge=True)
async def get_full_collection():
    db = firestore.AsyncClient()
    # [START firestore_data_get_all_documents_async]
    docs = db.collection("cities").stream()

    async for doc in docs:
        print(f"{doc.id} => {doc.to_dict()}")
async def list_document_subcollections():
    db = firestore.AsyncClient()
    # [START firestore_data_get_sub_collections_async]
    collections = db.collection("cities").document("SF").collections()
    async for collection in collections:
        async for doc in collection.stream():
            print(f"{doc.id} => {doc.to_dict()}")
async def structure_doc_ref_alternate():
    db = firestore.AsyncClient()
    # [START firestore_data_reference_document_path_async]
    a_lovelace_ref = db.document("users/alovelace")
    # [END firestore_data_reference_document_path_async]

    return a_lovelace_ref
async def add_from_dict():
    db = firestore.AsyncClient()
    # [START firestore_data_set_from_map_async]
    data = {"name": "Los Angeles", "state": "CA", "country": "USA"}

    # Add a new doc in collection 'cities' with ID 'LA'
    await db.collection("cities").document("LA").set(data)
async def structure_subcollection_ref():
    db = firestore.AsyncClient()
    # [START firestore_data_reference_subcollection_async]
    room_a_ref = db.collection("rooms").document("roomA")
    message_ref = room_a_ref.collection("messages").document("message1")
    # [END firestore_data_reference_subcollection_async]
    print(message_ref)
Example #9
0
async def structure_doc_ref():
    db = firestore.AsyncClient()
    # [START structure_doc_ref_async]
    # [START firestore_data_reference_document_async]
    a_lovelace_ref = db.collection("users").document("alovelace")
    # [END firestore_data_reference_document_async]
    # [END structure_doc_ref_async]
    print(a_lovelace_ref)
async def quickstart_get_collection():
    db = firestore.AsyncClient()
    # [START firestore_quickstart_get_collection_async]
    users_ref = db.collection("users")
    docs = users_ref.stream()

    async for doc in docs:
        print(f"{doc.id} => {doc.to_dict()}")
async def order_limit_to_last():
    db = firestore.AsyncClient()
    # [START firestore_query_order_limit_async]
    cities_ref = db.collection("cities")
    query = cities_ref.order_by("name").limit_to_last(2)
    results = await query.get()
    # [END firestore_query_order_limit_async]
    print(results)
async def compound_query_single_clause():
    db = firestore.AsyncClient()
    # [START firestore_query_filter_single_examples_async]
    cities_ref = db.collection("cities")

    cities_ref.where("state", "==", "CA")
    cities_ref.where("population", "<", 1000000)
    cities_ref.where("name", ">=", "San Francisco")
async def order_where_valid():
    db = firestore.AsyncClient()
    # [START firestore_query_order_with_filter_async]
    cities_ref = db.collection("cities")
    query = cities_ref.where("population", ">", 2500000).order_by("population")
    results = query.stream()
    # [END firestore_query_order_with_filter_async]
    print([d async for d in results])
Example #14
0
async def structure_collection_ref():
    db = firestore.AsyncClient()
    # [START structure_collection_ref_async]
    # [START firestore_data_reference_collection_async]
    users_ref = db.collection("users")
    # [END firestore_data_reference_collection_async]
    # [END structure_collection_ref_async]
    print(users_ref)
async def get_simple_query():
    db = firestore.AsyncClient()
    # [START firestore_data_query_async]
    # Note: Use of CollectionRef stream() is prefered to get()
    docs = db.collection("cities").where("capital", "==", True).stream()

    async for doc in docs:
        print(f"{doc.id} => {doc.to_dict()}")
async def get_custom_class():
    db = firestore.AsyncClient()
    # [START firestore_data_get_as_custom_type_async]
    doc_ref = db.collection("cities").document("BJ")

    doc = await doc_ref.get()
    city = City.from_dict(doc.to_dict())
    print(city)
async def order_where_invalid():
    db = firestore.AsyncClient()
    # [START firestore_query_order_field_invalid_async]
    cities_ref = db.collection("cities")
    query = cities_ref.where("population", ">", 2500000).order_by("country")
    results = query.stream()
    # [END firestore_query_order_field_invalid_async]
    print(results)
async def compound_query_simple():
    db = firestore.AsyncClient()
    # [START firestore_query_filter_eq_boolean_async]
    cities_ref = db.collection("cities")

    query = cities_ref.where("capital", "==", True)
    # [END firestore_query_filter_eq_boolean_async]

    print(query)
async def add_new_doc():
    db = firestore.AsyncClient()
    # [START firestore_data_set_id_random_document_ref_async]
    new_city_ref = db.collection("cities").document()

    # later...
    await new_city_ref.set({
        # ...
    })
Example #20
0
async def add_new_doc():
    db = firestore.AsyncClient()
    # [START add_new_doc_async]
    new_city_ref = db.collection("cities").document()

    # later...
    await new_city_ref.set({
        # ...
    })
Example #21
0
async def cursor_simple_start_at():
    db = firestore.AsyncClient()
    # [START cursor_simple_start_at_async]
    cities_ref = db.collection("cities")
    query_start_at = cities_ref.order_by("population").start_at(
        {"population": 1000000})
    # [END cursor_simple_start_at_async]

    return query_start_at
async def order_simple_limit_desc():
    db = firestore.AsyncClient()
    # [START firestore_query_order_desc_limit_async]
    cities_ref = db.collection("cities")
    query = cities_ref.order_by("name",
                                direction=firestore.Query.DESCENDING).limit(3)
    results = query.stream()
    # [END firestore_query_order_desc_limit_async]
    print(results)
Example #23
0
async def quickstart_new_instance():
    # [START quickstart_new_instance_async]
    from google.cloud import firestore

    # Project ID is determined by the GCLOUD_PROJECT environment variable
    db = firestore.AsyncClient()
    # [END quickstart_new_instance_async]

    return db
def fs_client():
    yield firestore.AsyncClient()

    # clean up
    for shard in shards_list:
        shard.delete()

    if doc_ref:
        doc_ref.delete()
Example #25
0
async def order_where_limit():
    db = firestore.AsyncClient()
    # [START order_where_limit_async]
    cities_ref = db.collection("cities")
    query = cities_ref.where("population", ">",
                             2500000).order_by("population").limit(2)
    results = query.stream()
    # [END order_where_limit_async]
    print([d async for d in results])
async def cursor_simple_end_at():
    db = firestore.AsyncClient()
    # [START firestore_query_cursor_end_at_field_value_single_async]
    cities_ref = db.collection("cities")
    query_end_at = cities_ref.order_by("population").end_at(
        {"population": 1000000})
    # [END firestore_query_cursor_end_at_field_value_single_async]

    return query_end_at
async def quickstart_add_data_two():
    db = firestore.AsyncClient()
    # [START firestore_setup_dataset_pt2_async]
    doc_ref = db.collection("users").document("aturing")
    await doc_ref.set({
        "first": "Alan",
        "middle": "Mathison",
        "last": "Turing",
        "born": 1912
    })
async def get_check_exists():
    db = firestore.AsyncClient()
    # [START firestore_data_get_as_map_async]
    doc_ref = db.collection("cities").document("SF")

    doc = await doc_ref.get()
    if doc.exists:
        print(f"Document data: {doc.to_dict()}")
    else:
        print("No such document!")
async def array_contains_filter():
    db = firestore.AsyncClient()
    # [START firestore_query_filter_array_contains_async]
    cities_ref = db.collection("cities")

    query = cities_ref.where("regions", "array_contains", "west_coast")
    # [END firestore_query_filter_array_contains_async]
    docs = query.stream()
    async for doc in docs:
        print(f"{doc.id} => {doc.to_dict()}")
async def update_doc():
    db = firestore.AsyncClient()
    await db.collection("cities").document("DC").set(
        City("Washington D.C.", None, "USA", True, 680000,
             ["east_coast"]).to_dict())
    # [START firestore_data_set_field_async]
    city_ref = db.collection("cities").document("DC")

    # Set the capital field
    await city_ref.update({"capital": True})