@@ -603,6 +603,112 @@ def cursor_paginate():
603
603
return next_query
604
604
605
605
606
+ def listen_document ():
607
+ db = firestore .Client ()
608
+ # [START listen_document]
609
+
610
+ # Create a callback on_snapshot function to capture changes
611
+ def on_snapshot (doc_snapshot ):
612
+ print (u'Received doc snapshot: {}' .format (doc_snapshot .to_dict ()))
613
+
614
+ doc_ref = db .collection (u'cities' ).document (u'SF' )
615
+
616
+ # Watch the document
617
+ doc_watch = doc_ref .on_snapshot (on_snapshot )
618
+ # [END listen_document]
619
+
620
+ print (u'Creating document' )
621
+ data = {
622
+ u'name' : u'San Francisco' ,
623
+ u'state' : u'CA' ,
624
+ u'country' : u'USA' ,
625
+ u'capital' : False ,
626
+ u'population' : 860000
627
+ }
628
+ db .collection (u'cities' ).document (u'SF' ).set (data )
629
+
630
+ # [START detach_listener]
631
+ # Terminate watch on a document
632
+ doc_watch .unsubscribe ()
633
+ # [START detach_listener]
634
+
635
+
636
+ def listen_multiple ():
637
+ db = firestore .Client ()
638
+ # [START listen_multiple]
639
+
640
+ # Create a callback on_snapshot function to capture changes
641
+ def on_snapshot (col_snapshot ):
642
+ print (u'Callback received query snapshot.' )
643
+ print (u'Current cities in California: ' )
644
+ for doc in col_snapshot .documents :
645
+ print (u'{} => {}' .format (doc .id ))
646
+
647
+ col_query = db .collection (u'cities' ).where (u'state' , u'==' , u'CA' )
648
+
649
+ # Watch the collection query
650
+ query_watch = col_query .on_snapshot (on_snapshot )
651
+
652
+ # [END listen_multiple]
653
+ mtv_document = db .collection (u'cities' ).document (u'MTV' )
654
+ print (u'Creating document' )
655
+ mtv_document .set ({
656
+ u'name' : u'Mountain View' ,
657
+ u'state' : u'CA' ,
658
+ u'country' : u'USA' ,
659
+ u'capital' : False ,
660
+ u'population' : 80000
661
+ })
662
+
663
+ print (u'Modifying document' )
664
+ mtv_document .update ({
665
+ u'name' : u'Mountain View' ,
666
+ u'state' : u'CA' ,
667
+ u'country' : u'USA' ,
668
+ u'capital' : False ,
669
+ u'population' : 90000
670
+ })
671
+
672
+ print (u'Delete document' )
673
+ mtv_document .delete ()
674
+ query_watch .unsubscribe ()
675
+
676
+
677
+ def listen_for_changes ():
678
+ db = firestore .Client ()
679
+ # [START listen_for_changes]
680
+
681
+ # Create a callback on_snapshot function to capture changes
682
+ def on_snapshot (col_snapshot , changes ):
683
+ print (u'Callback received query snapshot.' )
684
+ print (u'Current cities in California: ' )
685
+ for change in changes :
686
+ if change .type .name == "ADDED" :
687
+ print (u'New city: {}' .format (change .document .data ()))
688
+ elif change .type .name == "MODIFIED" :
689
+ print (u'Modified city: {}' .format (change .document .data ()))
690
+ elif change .type .name == "REMOVED" :
691
+ print (u'Removed city: {}' .format (change .document .data ()))
692
+
693
+ col_query = db .collection (u'cities' ).where (u'state' , u'==' , u'CA' )
694
+
695
+ # Watch the collection query
696
+ query_watch = col_query .on_snapshot (on_snapshot )
697
+
698
+ # [END listen_for_changes]
699
+ print (u'Creating document' )
700
+ data = {
701
+ u'name' : u'San Francisco' ,
702
+ u'state' : u'CA' ,
703
+ u'country' : u'USA' ,
704
+ u'capital' : False ,
705
+ u'population' : 860000
706
+ }
707
+ db .collection (u'cities' ).document (u'SF' ).set (data )
708
+
709
+ query_watch .unsubscribe ()
710
+
711
+
606
712
def cursor_multiple_conditions ():
607
713
db = firestore .Client ()
608
714
# [START cursor_multiple_conditions]
0 commit comments