本文整理汇总了Python中google.cloud.firestore.Client方法的典型用法代码示例。如果您正苦于以下问题:Python firestore.Client方法的具体用法?Python firestore.Client怎么用?Python firestore.Client使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类google.cloud.firestore
的用法示例。
在下文中一共展示了firestore.Client方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: add_example_data
# 需要导入模块: from google.cloud import firestore [as 别名]
# 或者: from google.cloud.firestore import Client [as 别名]
def add_example_data():
db = firestore.Client()
# [START add_example_data]
cities_ref = db.collection(u'cities')
cities_ref.document(u'BJ').set(
City(u'Beijing', None, u'China', True, 21500000, [u'hebei']).to_dict())
cities_ref.document(u'SF').set(
City(u'San Francisco', u'CA', u'USA', False, 860000,
[u'west_coast', u'norcal']).to_dict())
cities_ref.document(u'LA').set(
City(u'Los Angeles', u'CA', u'USA', False, 3900000,
[u'west_coast', u'socal']).to_dict())
cities_ref.document(u'DC').set(
City(u'Washington D.C.', None, u'USA', True, 680000,
[u'east_coast']).to_dict())
cities_ref.document(u'TOK').set(
City(u'Tokyo', None, u'Japan', True, 9000000,
[u'kanto', u'honshu']).to_dict())
# [END add_example_data]
示例2: update_doc_array
# 需要导入模块: from google.cloud import firestore [as 别名]
# 或者: from google.cloud.firestore import Client [as 别名]
def update_doc_array():
db = firestore.Client()
db.collection(u'cities').document(u'DC').set(
City(u'Washington D.C.', None, u'USA', True, 680000,
[u'east_coast']).to_dict())
# [START fs_update_doc_array]
city_ref = db.collection(u'cities').document(u'DC')
# Atomically add a new region to the 'regions' array field.
city_ref.update({u'regions': firestore.ArrayUnion([u'greater_virginia'])})
# // Atomically remove a region from the 'regions' array field.
city_ref.update({u'regions': firestore.ArrayRemove([u'east_coast'])})
# [END fs_update_doc_array]
city = city_ref.get()
print(f'Updated the regions field of the DC. {city.to_dict()}')
示例3: update_nested
# 需要导入模块: from google.cloud import firestore [as 别名]
# 或者: from google.cloud.firestore import Client [as 别名]
def update_nested():
db = firestore.Client()
# [START update_nested]
# Create an initial document to update
frank_ref = db.collection(u'users').document(u'frank')
frank_ref.set({
u'name': u'Frank',
u'favorites': {
u'food': u'Pizza',
u'color': u'Blue',
u'subject': u'Recess'
},
u'age': 12
})
# Update age and favorite color
frank_ref.update({
u'age': 13,
u'favorites.color': u'Red'
})
# [END update_nested]
示例4: update_data_transaction_result
# 需要导入模块: from google.cloud import firestore [as 别名]
# 或者: from google.cloud.firestore import Client [as 别名]
def update_data_transaction_result():
db = firestore.Client()
# [START update_data_transaction_result]
transaction = db.transaction()
city_ref = db.collection(u'cities').document(u'SF')
@firestore.transactional
def update_in_transaction(transaction, city_ref):
snapshot = city_ref.get(transaction=transaction)
new_population = snapshot.get(u'population') + 1
if new_population < 1000000:
transaction.update(city_ref, {
u'population': new_population
})
return True
else:
return False
result = update_in_transaction(transaction, city_ref)
if result:
print(u'Population updated')
else:
print(u'Sorry! Population is too big.')
# [END update_data_transaction_result]
示例5: update_data_batch
# 需要导入模块: from google.cloud import firestore [as 别名]
# 或者: from google.cloud.firestore import Client [as 别名]
def update_data_batch():
db = firestore.Client()
# [START update_data_batch]
batch = db.batch()
# Set the data for NYC
nyc_ref = db.collection(u'cities').document(u'NYC')
batch.set(nyc_ref, {u'name': u'New York City'})
# Update the population for SF
sf_ref = db.collection(u'cities').document(u'SF')
batch.update(sf_ref, {u'population': 1000000})
# Delete DEN
den_ref = db.collection(u'cities').document(u'DEN')
batch.delete(den_ref)
# Commit the batch
batch.commit()
# [END update_data_batch]
示例6: cursor_multiple_conditions
# 需要导入模块: from google.cloud import firestore [as 别名]
# 或者: from google.cloud.firestore import Client [as 别名]
def cursor_multiple_conditions():
db = firestore.Client()
# [START cursor_multiple_conditions]
start_at_name = (
db.collection(u'cities')
.order_by(u'name')
.order_by(u'state')
.start_at({
u'name': u'Springfield'
})
)
start_at_name_and_state = (
db.collection(u'cities')
.order_by(u'name')
.order_by(u'state')
.start_at({
u'name': u'Springfield',
u'state': u'Missouri'
})
)
# [END cursor_multiple_conditions]
return start_at_name, start_at_name_and_state
示例7: delete_full_collection
# 需要导入模块: from google.cloud import firestore [as 别名]
# 或者: from google.cloud.firestore import Client [as 别名]
def delete_full_collection():
db = firestore.Client()
# [START delete_full_collection]
def delete_collection(coll_ref, batch_size):
docs = coll_ref.limit(batch_size).stream()
deleted = 0
for doc in docs:
print(f'Deleting doc {doc.id} => {doc.to_dict()}')
doc.reference.delete()
deleted = deleted + 1
if deleted >= batch_size:
return delete_collection(coll_ref, batch_size)
# [END delete_full_collection]
delete_collection(db.collection(u'cities'), 10)
delete_collection(db.collection(u'data'), 10)
delete_collection(db.collection(u'objects'), 10)
delete_collection(db.collection(u'users'), 10)
示例8: test_invocations
# 需要导入模块: from google.cloud import firestore [as 别名]
# 或者: from google.cloud.firestore import Client [as 别名]
def test_invocations():
db = firestore.Client()
main.db = db
translations = db.collection('translations')
clear_collection(translations)
event = {
'data': base64.b64encode(json.dumps({
'Original': 'My test message',
'Language': 'de',
}).encode('utf-8'))
}
main.translate_message(event, None)
docs = [doc for doc in translations.stream()]
assert len(docs) == 1 # Should be only the one just created
message = docs[0].to_dict()
assert message['Original'] == 'My test message'
assert message['Language'] == 'de'
assert len(message['Translated']) > 0
assert message['OriginalLanguage'] == 'en'
示例9: db
# 需要导入模块: from google.cloud import firestore [as 别名]
# 或者: from google.cloud.firestore import Client [as 别名]
def db():
def clear_collection(collection):
""" Removes every document from the collection, to make it easy to see
what has been added by the current test run.
"""
for doc in collection.stream():
doc.reference.delete()
client = firestore.Client()
translations = client.collection('translations')
clear_collection(translations)
translations.add({
'Original': 'A testing message',
'Language': 'fr',
'Translated': '"A testing message", but in French',
'OriginalLanguage': 'en',
},
document_id='test translation'
)
yield client
示例10: next_page
# 需要导入模块: from google.cloud import firestore [as 别名]
# 或者: from google.cloud.firestore import Client [as 别名]
def next_page(limit=10, start_after=None):
db = firestore.Client()
query = db.collection(u'Book').limit(limit).order_by(u'title')
if start_after:
# Construct a new query starting at this document.
query = query.start_after({u'title': start_after})
docs = query.stream()
docs = list(map(document_to_dict, docs))
last_title = None
if limit == len(docs):
# Get the last document from the results and set as the last title.
last_title = docs[-1][u'title']
return docs, last_title
示例11: main
# 需要导入模块: from google.cloud import firestore [as 别名]
# 或者: from google.cloud.firestore import Client [as 别名]
def main():
client = Client()
for collection in client.collections():
zap_collection(collection)
示例12: quickstart_new_instance
# 需要导入模块: from google.cloud import firestore [as 别名]
# 或者: from google.cloud.firestore import Client [as 别名]
def quickstart_new_instance():
# [START quickstart_new_instance]
from google.cloud import firestore
# Project ID is determined by the GCLOUD_PROJECT environment variable
db = firestore.Client()
# [END quickstart_new_instance]
return db
示例13: quickstart_add_data_one
# 需要导入模块: from google.cloud import firestore [as 别名]
# 或者: from google.cloud.firestore import Client [as 别名]
def quickstart_add_data_one():
db = firestore.Client()
# [START quickstart_add_data_one]
doc_ref = db.collection(u'users').document(u'alovelace')
doc_ref.set({
u'first': u'Ada',
u'last': u'Lovelace',
u'born': 1815
})
# [END quickstart_add_data_one]
示例14: quickstart_add_data_two
# 需要导入模块: from google.cloud import firestore [as 别名]
# 或者: from google.cloud.firestore import Client [as 别名]
def quickstart_add_data_two():
db = firestore.Client()
# [START quickstart_add_data_two]
doc_ref = db.collection(u'users').document(u'aturing')
doc_ref.set({
u'first': u'Alan',
u'middle': u'Mathison',
u'last': u'Turing',
u'born': 1912
})
# [END quickstart_add_data_two]
示例15: quickstart_get_collection
# 需要导入模块: from google.cloud import firestore [as 别名]
# 或者: from google.cloud.firestore import Client [as 别名]
def quickstart_get_collection():
db = firestore.Client()
# [START quickstart_get_collection]
users_ref = db.collection(u'users')
docs = users_ref.stream()
for doc in docs:
print(f'{doc.id} => {doc.to_dict()}')
# [END quickstart_get_collection]