2017-11-01 3 views
1

Firestore를 사용하여 대기열로 작동하는 응용 프로그램이 있습니다. 다른 출처의 데이터가 Firestore에 /data/{id}으로 저장되고 unprocessed이 true로 설정됩니다. 내 Node.js 스크립트에서 이러한 처리되지 않은 레코드를 쿼리하여 천천히 처리하려고합니다. 수십만 개의 레코드가 있으므로 메모리에로드하려고하면 프로세스가 충돌합니다.Firestore 쿼리에서 .limit() 사용

코드 :

firestore.collection('data').where('unprocessed', '==', true).limit(25).onSnapshot((snapshot) => { 
    snapshot.forEach((doc) => { 
     processItem(doc); 
    }); 
    }); 

processItem() 기능이 false로 설정 unprocessed 속성을 다시 경우 FireStore에 데이터를 저장, 필요한 처리를 수행합니다. 그것을 실행하게 내 코드에서 .limit() 제거

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) 

,하지만 내가 실행

나는에 실행 해요 문제는 시간이 나는 다음과 같은 오류가이 코드를 실행하려고한다는 것입니다 항목을 처리하고 저장하는 함수가 제대로 끝나지 않은 또 다른 문제로, 프로세스가 충돌 할 때까지 내 메모리 사용량이 계속 증가합니다.

내 첫 번째 본능은 .limit()이 어떤 이유로 든 .onSnapshot()과 호환되지 않는다고 생각하는 것일 수 있지만, 어쩌면 누군가가 나에게 무슨 일이 일어나고 있는지 더 잘 알 수 있습니다.

편집 나는 또한 https://firebase.google.com/docs/firestore/manage-data/delete-data 같이합니다 ('삭제 컬렉션'섹션에서), .orderBy('__name__')에 추가하려고 시도했지만, 이것은 단지 또 다른 오류가 발생

:

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) 
+1

실험으로'.orderBy ('unprocessed')'를 쿼리에 추가하십시오. –

+0

@BobSnyder'.orderBy ('unprocessed')'를 추가하면 약간 다른 오류가 발생합니다 :'Error : Error 3 : order by 절은 동일하지 않은 필터가있는 필드를 포함 할 수 없습니다' –

+1

'.orderBy (...)'을 사용하여 작업 할 수 있습니다 (https://firebase.google.com/docs/firestore/query-data/order-limit-data). –

답변

2

하는 경우 .orderBy(FirebaseFirestore.FieldPath.documentId())을 추가하면 문제가 해결됩니다. 이 문제가 왜 수정되는지에 대한 간단한 설명은 here입니다. 다음은 gist 예의 예입니다.

관련 문제