title: Using .limit() with Firestore queries
body: <p>I've got an application that acts as a queue using Firestore. Data comes in from another source and is stored in Firestore at <code>/data/{id}</code>, with a property <code>unprocessed</code> set to true. In my Node.js script, I'm wanting to query these unprocessed records a few at a time to process them slowly. There are hundreds of thousands of records, so attempting to load them all into memory just crashes the process.</p>
  
  <p>Code:</p>
  
  <pre><code>firestore.collection('data').where('unprocessed', '==', true).limit(25).onSnapshot((snapshot) =&gt; {
      snapshot.forEach((doc) =&gt; {
        processItem(doc);
      });
    });
  </code></pre>
  
  <p>The <code>processItem()</code> function does the necessary processing, saving the data back to Firestore with the <code>unprocessed</code> property set to false.</p>
  
  <p>The problem I'm running in to is that any time I try to run this code, I get the following error:</p>
  
  <pre><code>Error: Error 3: Order must include __name__
      at sendError (C:\[myApp]\node_modules\@google-cloud\firestore\src\watch.js:254:15)
      at DestroyableTransform.stream.on.proto (C:\[myApp]\node_modules\@google-cloud\firestore\src\watch.js:532:13)
      at emitOne (events.js:115:13)
      at DestroyableTransform.emit (events.js:210:7)
      at addChunk (C:\[myApp]\node_modules\readable-stream\lib\_stream_readable.js:284:12)
      at readableAddChunk (C:\[myApp]\node_modules\readable-stream\lib\_stream_readable.js:271:11)
      at DestroyableTransform.Readable.push (C:\[myApp]\node_modules\readable-stream\lib\_stream_readable.js:238:10)
      at DestroyableTransform.Transform.push (C:\[myApp]\node_modules\readable-stream\lib\_stream_transform.js:146:32)
      at afterTransform (C:\[myApp]\node_modules\readable-stream\lib\_stream_transform.js:102:51)
      at TransformState.afterTransform (C:\[myApp]\node_modules\readable-stream\lib\_stream_transform.js:79:12)
      at DestroyableTransform.noop [as _transform] (C:\[myApp]\node_modules\through2\through2.js:26:3)
      at DestroyableTransform.Transform._read (C:\[myApp]\node_modules\readable-stream\lib\_stream_transform.js:182:10)
      at DestroyableTransform.Transform._write (C:\[myApp]\node_modules\readable-stream\lib\_stream_transform.js:170:83)
      at doWrite (C:\[myApp]\node_modules\readable-stream\lib\_stream_writable.js:406:64)
      at writeOrBuffer (C:\[myApp]\node_modules\readable-stream\lib\_stream_writable.js:395:5)
      at DestroyableTransform.Writable.write (C:\[myApp]\node_modules\readable-stream\lib\_stream_writable.js:322:11)
  </code></pre>
  
  <p>Removing the <code>.limit()</code> from my code makes it run, but then I run into another issue where the function to process and store the item back isn't finishing properly, and my memory usage just keeps growing until the process crashes.</p>
  
  <p>My first instinct is to think that <code>.limit()</code> isn't compatible with <code>.onSnapshot()</code> for some reason, but perhaps someone can give me a better idea of what's going on here.</p>
  
  <h1>Edit</h1>
  
  <p>I've also attempted to add in <code>.orderBy('__name__')</code>, as shown in <a href="https://firebase.google.com/docs/firestore/manage-data/delete-data" rel="nofollow noreferrer">https://firebase.google.com/docs/firestore/manage-data/delete-data</a> (in the 'delete collections' section), but this just results in yet another error:</p>
  
  <pre><code>Error: Trying to compare documents on fields that don't exist. Please include the fields you are ordering on in your select() call.
      at C:\[myApp]\node_modules\@google-cloud\firestore\src\reference.js:1679:19
      at Array.sort (native)
      at computeSnapshot (C:\[myApp]\node_modules\@google-cloud\firestore\src\watch.js:438:20)
      at push (C:\[myApp]\node_modules\@google-cloud\firestore\src\watch.js:469:18)
      at DestroyableTransform.stream.on.proto (C:\[myApp]\node_modules\@google-cloud\firestore\src\watch.js:514:15)
      at emitOne (events.js:115:13)
      at DestroyableTransform.emit (events.js:210:7)
      at addChunk (C:\[myApp]\node_modules\readable-stream\lib\_stream_readable.js:284:12)
      at readableAddChunk (C:\[myApp]\node_modules\readable-stream\lib\_stream_readable.js:271:11)
      at DestroyableTransform.Readable.push (C:\[myApp]\node_modules\readable-stream\lib\_stream_readable.js:238:10)
      at DestroyableTransform.Transform.push (C:\[myApp]\node_modules\readable-stream\lib\_stream_transform.js:146:32)
      at afterTransform (C:\[myApp]\node_modules\readable-stream\lib\_stream_transform.js:102:51)
      at TransformState.afterTransform (C:\[myApp]\node_modules\readable-stream\lib\_stream_transform.js:79:12)
      at DestroyableTransform.noop [as _transform] (C:\[myApp]\node_modules\through2\through2.js:26:3)
      at DestroyableTransform.Transform._read (C:\[myApp]\node_modules\readable-stream\lib\_stream_transform.js:182:10)
      at DestroyableTransform.Transform._write (C:\[myApp]\node_modules\readable-stream\lib\_stream_transform.js:170:83)
  </code></pre>