<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: yuyu</title>
    <description>The latest articles on DEV Community by yuyu (@mtomto_tech).</description>
    <link>https://dev.to/mtomto_tech</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1568133%2F435f0e81-ac5e-4608-96fd-2e869ddaacc6.png</url>
      <title>DEV Community: yuyu</title>
      <link>https://dev.to/mtomto_tech</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mtomto_tech"/>
    <language>en</language>
    <item>
      <title>Get Random Documents from Firestore</title>
      <dc:creator>yuyu</dc:creator>
      <pubDate>Mon, 03 Jun 2024 15:22:05 +0000</pubDate>
      <link>https://dev.to/mtomto_tech/get-random-documents-from-firestore-4c65</link>
      <guid>https://dev.to/mtomto_tech/get-random-documents-from-firestore-4c65</guid>
      <description>&lt;p&gt;When working with Firestore, retrieving random elements poses a challenge because Firestore does not natively support queries for fetching documents randomly.&lt;/p&gt;

&lt;p&gt;In addition, Firestore does not support offset queries, so it is not possible to generate a random number on the client side and then fetch documents randomly by specifying an index.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Basic Solution&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Fetch all documents in the collection, shuffle the array, and then select elements either from the beginning or at random indexes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This method is quite simple. However, if there are a large number of documents, it can be quite costly and the processing speed may become slow.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Another Approach: Changing the Way to Manage Data&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is a solution that only loads the necessary records.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Configure a property within each document that has a number assigned randomly.&lt;/li&gt;
&lt;li&gt;When fetching documents, use an &lt;code&gt;orderBy&lt;/code&gt; query targeting this random number property. By combining this with a &lt;code&gt;limit&lt;/code&gt; query, it is possible to fetch a specified number of documents or even the entire collection.&lt;/li&gt;
&lt;li&gt;After fetching the documents, assign a new random number to the random number property of each document. This step ensures that the collection remains shuffled reproducibly.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2uvd4gp4tdbflvg7vh8z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2uvd4gp4tdbflvg7vh8z.png" alt="firestore console"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Bottleneck&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This method requires a significant number of write operations. Since Firestore incurs higher costs for write operations compared to read or delete operations, frequent fetching can become costly.&lt;/p&gt;

&lt;p&gt;See also: &lt;a href="https://firebase.google.com/docs/firestore/pricing" rel="noopener noreferrer"&gt;https://firebase.google.com/docs/firestore/pricing&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Finally&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I was pleasantly surprised when I first discovered this method. Despite Firestore not supplying direct queries for fetching random documents, this creative solution offers an excellent workaround.&lt;/p&gt;

</description>
      <category>firebase</category>
      <category>nosql</category>
      <category>algorithms</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
