Skip to content

Commit 4e69e24

Browse files
docs(firestore): guide for complex queries
1 parent 6951e69 commit 4e69e24

File tree

1 file changed

+251
-0
lines changed

1 file changed

+251
-0
lines changed

docs/guide/firestore.rst

Lines changed: 251 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,254 @@ method.
199199
200200
fsdb.collection('Marvels').document('Movies').delete()
201201
..
202+
203+
204+
Complex Queries
205+
---------------
206+
207+
order_by
208+
^^^^^^^^
209+
210+
To fetch documents with it's data in a collection ``Marvels``, ordered
211+
of field ``year``-s value.
212+
213+
.. code-block:: python
214+
215+
fsdb.collection('Marvels').order_by('year').get()
216+
..
217+
218+
219+
220+
To order the documents in descending order of field ``year``s value
221+
, add ``direction`` keyword argument.
222+
223+
.. code-block:: python
224+
225+
from google.cloud.firestore import Query
226+
227+
fsdb.collection('Marvels').order_by('year', direction=Query.DESCENDING).get()
228+
..
229+
230+
231+
limit_to_first
232+
^^^^^^^^^^^^^^
233+
234+
To limit the number of documents returned in a query to first *N*
235+
documents, we use ``limit_to_first`` method.
236+
237+
.. code-block:: python
238+
239+
docs = fsdb.collection('Marvels').order_by('year', direction='DESCENDING').limit_to_first(2).get()
240+
..
241+
242+
.. note::
243+
`limit_to_first` and `limit_to_last` are mutually
244+
exclusive. Setting `limit_to_first` will drop
245+
previously set `limit_to_last`.
246+
247+
248+
limit_to_last
249+
^^^^^^^^^^^^^
250+
251+
To limit the number of documents returned in a query to last *N*
252+
documents, we use ``limit_to_last`` method.
253+
254+
.. code-block:: python
255+
256+
docs = fsdb.collection('Marvels').order_by('year', direction='ASCENDING').limit_to_last(2).get()
257+
..
258+
259+
.. note::
260+
`limit_to_first` and `limit_to_last` are mutually
261+
exclusive. Setting `limit_to_first` will drop
262+
previously set `limit_to_last`.
263+
264+
265+
start_at
266+
^^^^^^^^
267+
268+
To fetch documents with field ``year`` with a ``2007`` or higher will
269+
be fetched from a collection ``Marvels``, and anything before ``2007``
270+
will be ignored.
271+
272+
.. code-block:: python
273+
274+
docs = fsdb.collection('Marvels').order_by('year').start_at({'year': 2007}).get()
275+
..
276+
277+
278+
start_after
279+
^^^^^^^^^^^
280+
281+
To fetch documents with field ``year`` with a value greater than
282+
``2007`` will be fetched from a collection ``Marvels``, and any
283+
document with a value ``2007`` or less will be ignored.
284+
285+
.. code-block:: python
286+
287+
docs = fsdb.collection('Marvels').order_by('year').start_after({'year': 2007}).get()
288+
..
289+
290+
291+
end_at
292+
^^^^^^
293+
294+
To fetch documents with field ``year`` with a ``2022`` or less will
295+
be fetched from a collection ``Marvels``, and anything after ``2022``
296+
will be ignored.
297+
298+
.. code-block:: python
299+
300+
docs = fsdb.collection('Marvels').order_by('year').end_at({'year': 2022}).get()
301+
..
302+
303+
304+
end_before
305+
^^^^^^^^^^
306+
307+
To fetch documents with field ``year`` with a value less than
308+
``2023`` will be fetched from a collection ``Marvels``, and any
309+
document with a value ``2023`` or greater will be ignored.
310+
311+
.. code-block:: python
312+
313+
docs = fsdb.collection('Marvels').order_by('year').end_before({'year': 2007}).get()
314+
..
315+
316+
317+
offset
318+
^^^^^^
319+
320+
To filter out the first *N* documents from a query in collection
321+
``Marvels``.
322+
323+
.. code-block:: python
324+
325+
docs = fsdb.collection('Marvels').order_by('year').offset(5).get()
326+
..
327+
328+
329+
select
330+
^^^^^^
331+
332+
To filter the fields ``lead.nam`` and ``released`` to be returned from
333+
documents in collection ``Marvels``.
334+
335+
.. code-block:: python
336+
337+
docs = fsdb.collection('Marvels').select(['lead.name', 'released']).get()
338+
..
339+
340+
341+
where
342+
^^^^^
343+
344+
To fetch all documents and its data in a collection ``Marvels`` where
345+
a field ``year`` exists with a value less than ``2008``.
346+
347+
.. code-block:: python
348+
349+
fsdb.collection('Marvels').where('year', '<', 2008).get()
350+
..
351+
352+
353+
354+
To fetch all documents and its data in a collection ``Marvels`` where
355+
a field ``year`` exists with a value less than equal to ``2008``.
356+
357+
.. code-block:: python
358+
359+
fsdb.collection('Marvels').where('year', '<=', 2008).get()
360+
..
361+
362+
363+
364+
To fetch all documents and its data in a collection ``Marvels`` where
365+
a field ``released`` exists with a value equal to ``True``.
366+
367+
.. code-block:: python
368+
369+
fsdb.collection('Marvels').where('released', '==', True).get()
370+
..
371+
372+
373+
374+
To fetch all documents and its data in a collection ``Marvels`` where
375+
a field ``released`` exists with a value not equal to ``False``.
376+
377+
.. code-block:: python
378+
379+
fsdb.collection('Marvels').where('released', '!=', False).get()
380+
..
381+
382+
383+
384+
To fetch all documents and its data in a collection ``Marvels`` where
385+
a field ``year`` exists with a value greater than equal to ``2008``.
386+
387+
.. code-block:: python
388+
389+
fsdb.collection('Marvels').where('year', '>=', 2008).get()
390+
..
391+
392+
393+
To fetch all documents and its data in a collection ``Marvels`` where
394+
a field ``year`` exists with a value greater than ``2008``.
395+
396+
.. code-block:: python
397+
398+
fsdb.collection('Marvels').where('year', '>', 2008).get()
399+
..
400+
401+
402+
403+
To fetch all documents and its data in a collection ``Marvels`` where
404+
a array field ``cast`` exists and contains a value ``Gwyneth Paltrow``.
405+
406+
.. code-block:: python
407+
408+
fsdb.collection('Marvels').where('cast', 'array_contains', 'Gwyneth Paltrow').get()
409+
..
410+
411+
412+
413+
To fetch all documents and its data in a collection ``Marvels`` where
414+
a array field ``cast`` exists and contains either ``Gwyneth Paltrow``
415+
or ``Terrence Howard`` as a value.
416+
417+
.. code-block:: python
418+
419+
fsdb.collection('Marvels').where('cast', 'array_contains_any', ['Gwyneth Paltrow', 'Terrence Howard']).get()
420+
..
421+
422+
423+
424+
To fetch all documents and its data in a collection ``Marvels`` where
425+
a field ``lead.name`` exists with a value ``Robert Downey Jr.`` or
426+
``Benedict Cumberbatch``.
427+
428+
.. code-block:: python
429+
430+
fsdb.collection('Marvels').where('lead.name', 'in', ['Robert Downey Jr.', 'Benedict Cumberbatch']).get()
431+
..
432+
433+
434+
435+
To fetch all documents and its data in a collection ``Marvels`` where
436+
a field ``lead.name`` exists without a value ``Robert Downey Jr.`` or
437+
``Benedict Cumberbatch``.
438+
439+
.. code-block:: python
440+
441+
fsdb.collection('Marvels').where('lead.name', 'not-in', ['Robert Downey Jr.', 'Benedict Cumberbatch']).get()
442+
..
443+
444+
445+
446+
To fetch all documents and its data in a collection ``Marvels`` where
447+
a array field ``cast`` exists with a value ``Gwyneth Paltrow``.
448+
449+
.. code-block:: python
450+
451+
fsdb.collection('Marvels').where('cast', 'in', [['Gwyneth Paltrow']]).get()
452+
..

0 commit comments

Comments
 (0)