|
| 1 | +Firestore |
| 2 | +========= |
| 3 | + |
| 4 | +The firestore service allows you to run CRUD operations to your Firebase Firestore |
| 5 | +Database. |
| 6 | + |
| 7 | +.. code-block:: python |
| 8 | +
|
| 9 | + # Create database instance |
| 10 | + fsdb = firebaseApp.firestore() |
| 11 | +.. |
| 12 | +
|
| 13 | + .. note:: |
| 14 | + Each of the following methods accepts a user token: |
| 15 | + :ref:`get()<guide/firestore:get>`, :ref:`set()<guide/firestore:set>`, |
| 16 | + :ref:`update()<guide/firestore:update>`, and |
| 17 | + :ref:`delete()<guide/firestore:delete>`. |
| 18 | + |
| 19 | + |
| 20 | +Build Path |
| 21 | +---------- |
| 22 | + |
| 23 | +You can build paths to your data by using the ``collection()`` and ``document()`` method. |
| 24 | + |
| 25 | +.. code-block:: python |
| 26 | +
|
| 27 | + fsdb.collection('Marvels').document('Movies') |
| 28 | + fsdb.collection('Marvels').document('Movies').collection('PhaseOne').document('2008') |
| 29 | +
|
| 30 | +.. |
| 31 | +
|
| 32 | +Save Data |
| 33 | +--------- |
| 34 | + |
| 35 | +set |
| 36 | +^^^ |
| 37 | + |
| 38 | +To store data in a collection named ``Marvels`` and a document inside |
| 39 | +the collection named ``Movies``, use ``set()`` method. |
| 40 | + |
| 41 | +.. code-block:: python |
| 42 | +
|
| 43 | + data = { |
| 44 | + "name": "Iron Man", |
| 45 | + "lead": { |
| 46 | + "name": "Robert Downey Jr." |
| 47 | + }, |
| 48 | + 'released': False, |
| 49 | + } |
| 50 | +
|
| 51 | + fsdb.collection('Marvels').document('Movies').set(data) |
| 52 | +.. |
| 53 | +
|
| 54 | + .. attention:: |
| 55 | + Using this method on an existing document will overwrite the existing |
| 56 | + document. |
| 57 | + |
| 58 | + |
| 59 | +Read Data |
| 60 | +--------- |
| 61 | + |
| 62 | +get |
| 63 | +^^^ |
| 64 | + |
| 65 | +To read data from an existing document of an collection, use ``get()`` method. |
| 66 | + |
| 67 | +.. code-block:: python |
| 68 | +
|
| 69 | + fsdb.collection('Marvels').document('Movies').get() |
| 70 | +.. |
| 71 | +
|
| 72 | +It is possible to filter the data of an document to receive specific fields. |
| 73 | + |
| 74 | +.. code-block:: python |
| 75 | +
|
| 76 | + fsdb.collection('Marvels').document('Movies').get(field_paths=['lead.name', 'released']) |
| 77 | +
|
| 78 | + # Output: |
| 79 | + # {'lead': {'name': "Robert Downey Jr."}, 'released': False} |
| 80 | +.. |
| 81 | +
|
| 82 | + |
| 83 | + |
| 84 | +Update Data |
| 85 | +----------- |
| 86 | + |
| 87 | +update |
| 88 | +^^^^^^ |
| 89 | + |
| 90 | +To update existing data or add more data to an existing document, use |
| 91 | +``update()`` method. |
| 92 | + |
| 93 | +.. code-block:: python |
| 94 | +
|
| 95 | + # add new data to an existing document |
| 96 | +
|
| 97 | + data = { |
| 98 | + 'year': 2008, |
| 99 | + } |
| 100 | +
|
| 101 | + fsdb.collection('Marvels').document('Movies').update(data) |
| 102 | +.. |
| 103 | +
|
| 104 | +.. code-block:: python |
| 105 | +
|
| 106 | + # update data of an existing document |
| 107 | +
|
| 108 | + data = { |
| 109 | + 'released': True, |
| 110 | + } |
| 111 | +
|
| 112 | + fsdb.collection('Marvels').document('Movies').update(data) |
| 113 | +.. |
| 114 | +
|
| 115 | + |
| 116 | +Delete Data |
| 117 | +----------- |
| 118 | + |
| 119 | +delete |
| 120 | +^^^^^^ |
| 121 | + |
| 122 | +To remove an existing document in a collection, use ``delete()`` |
| 123 | +method. |
| 124 | + |
| 125 | +.. code-block:: python |
| 126 | +
|
| 127 | + fsdb.collection('Marvels').document('Movies').delete() |
| 128 | +.. |
0 commit comments