Firestore - Listen to specific field change?












15















How can I listen to a specific field change with firestore js sdk ?



In the documentation, they only seem to show how to listen for the whole document, if any of the "SF" field changes, it will trigger the callback.



db.collection("cities").doc("SF")
.onSnapshot(function(doc) {
console.log("Current data: ", doc && doc.data());
});









share|improve this question





























    15















    How can I listen to a specific field change with firestore js sdk ?



    In the documentation, they only seem to show how to listen for the whole document, if any of the "SF" field changes, it will trigger the callback.



    db.collection("cities").doc("SF")
    .onSnapshot(function(doc) {
    console.log("Current data: ", doc && doc.data());
    });









    share|improve this question



























      15












      15








      15


      1






      How can I listen to a specific field change with firestore js sdk ?



      In the documentation, they only seem to show how to listen for the whole document, if any of the "SF" field changes, it will trigger the callback.



      db.collection("cities").doc("SF")
      .onSnapshot(function(doc) {
      console.log("Current data: ", doc && doc.data());
      });









      share|improve this question
















      How can I listen to a specific field change with firestore js sdk ?



      In the documentation, they only seem to show how to listen for the whole document, if any of the "SF" field changes, it will trigger the callback.



      db.collection("cities").doc("SF")
      .onSnapshot(function(doc) {
      console.log("Current data: ", doc && doc.data());
      });






      javascript firebase google-cloud-firestore






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 18 '17 at 1:52









      Frank van Puffelen

      242k29387414




      242k29387414










      asked Dec 17 '17 at 23:40









      httpetehttpete

      2,60832233




      2,60832233
























          3 Answers
          3






          active

          oldest

          votes


















          16














          You can't. All operations in Firestore are on an entire document.



          This is also true for Cloud Functions Firestore triggers (you can only receive an entire document that's changed in some way).



          If you need to narrow the scope of some data to retrieve from a document, place that in a document within a subcollection, and query for that document individually.






          share|improve this answer



















          • 3





            Isn't that a big drawback compared to the firebase realtime database solution ?

            – httpete
            Dec 20 '17 at 22:19






          • 1





            Yes, this is known difference in behavior, and been discussed in various forums. If all you want to do is locate an existing key in the database and listen to changes, that's much easier in RTDB. In fact, there are a bunch of other known advantages (currently), documented at the end of this blog: firebase.googleblog.com/2017/10/…

            – Doug Stevenson
            Dec 20 '17 at 22:29






          • 1





            Does anyone know if they have plans to make this a feature? Especially with Cloud functions.firestore.document().field('myField/subFieldEtc').onUpdate()?

            – Jonathan002
            Jan 25 at 11:11





















          1














          Just in case you want to ignore events in some fields, you can do something like:



          export const yourCloudFunction = functions.firestore
          .document('/your-path')
          .onUpdate(
          field('foo', 'REMOVED', (change, context) => {
          console.log('Will get here only if foo was removed');
          // ... Your implementation here
          }),
          );


          I created a gist of the field function here. Remember, it is not avoiding your function to run if changes happen in other fields, it will just not handle that over to you and return a resolved promise instead.






          share|improve this answer































            0














            Listen for the document, then set a conditional on the field you're interesting in:



            firebase.firestore().collection('Dictionaries').doc('Spanish').collection('Words').doc(word).collection('Pronunciations').doc('Castilian-female-IBM').onSnapshot(function(snapshot) {
            if (snapshot.data().audioFiles) { // eliminates an error message
            if (snapshot.data().audioFiles.length === 2) {
            audioFilesReady++;
            if (audioFilesReady === 3) {
            $scope.showNextWord();
            }
            }
            }
            }, function(error) {
            console.error(error);
            });


            I'm listening for a document for a voice (Castilian-female-IBM), which contains an array of audio files, in webm and mp3 formats. When both of those audio files have come back asynchronously then snapshot.data().audioFiles.length === 2. This increments a conditional. When two more voices come back (Castilian-male-IBM and Latin_American-female-IBM) then audioFilesReady === 3 and the next function $scope.showNextWord() fires.






            share|improve this answer























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f47860531%2ffirestore-listen-to-specific-field-change%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              16














              You can't. All operations in Firestore are on an entire document.



              This is also true for Cloud Functions Firestore triggers (you can only receive an entire document that's changed in some way).



              If you need to narrow the scope of some data to retrieve from a document, place that in a document within a subcollection, and query for that document individually.






              share|improve this answer



















              • 3





                Isn't that a big drawback compared to the firebase realtime database solution ?

                – httpete
                Dec 20 '17 at 22:19






              • 1





                Yes, this is known difference in behavior, and been discussed in various forums. If all you want to do is locate an existing key in the database and listen to changes, that's much easier in RTDB. In fact, there are a bunch of other known advantages (currently), documented at the end of this blog: firebase.googleblog.com/2017/10/…

                – Doug Stevenson
                Dec 20 '17 at 22:29






              • 1





                Does anyone know if they have plans to make this a feature? Especially with Cloud functions.firestore.document().field('myField/subFieldEtc').onUpdate()?

                – Jonathan002
                Jan 25 at 11:11


















              16














              You can't. All operations in Firestore are on an entire document.



              This is also true for Cloud Functions Firestore triggers (you can only receive an entire document that's changed in some way).



              If you need to narrow the scope of some data to retrieve from a document, place that in a document within a subcollection, and query for that document individually.






              share|improve this answer



















              • 3





                Isn't that a big drawback compared to the firebase realtime database solution ?

                – httpete
                Dec 20 '17 at 22:19






              • 1





                Yes, this is known difference in behavior, and been discussed in various forums. If all you want to do is locate an existing key in the database and listen to changes, that's much easier in RTDB. In fact, there are a bunch of other known advantages (currently), documented at the end of this blog: firebase.googleblog.com/2017/10/…

                – Doug Stevenson
                Dec 20 '17 at 22:29






              • 1





                Does anyone know if they have plans to make this a feature? Especially with Cloud functions.firestore.document().field('myField/subFieldEtc').onUpdate()?

                – Jonathan002
                Jan 25 at 11:11
















              16












              16








              16







              You can't. All operations in Firestore are on an entire document.



              This is also true for Cloud Functions Firestore triggers (you can only receive an entire document that's changed in some way).



              If you need to narrow the scope of some data to retrieve from a document, place that in a document within a subcollection, and query for that document individually.






              share|improve this answer













              You can't. All operations in Firestore are on an entire document.



              This is also true for Cloud Functions Firestore triggers (you can only receive an entire document that's changed in some way).



              If you need to narrow the scope of some data to retrieve from a document, place that in a document within a subcollection, and query for that document individually.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Dec 17 '17 at 23:51









              Doug StevensonDoug Stevenson

              81.6k997115




              81.6k997115








              • 3





                Isn't that a big drawback compared to the firebase realtime database solution ?

                – httpete
                Dec 20 '17 at 22:19






              • 1





                Yes, this is known difference in behavior, and been discussed in various forums. If all you want to do is locate an existing key in the database and listen to changes, that's much easier in RTDB. In fact, there are a bunch of other known advantages (currently), documented at the end of this blog: firebase.googleblog.com/2017/10/…

                – Doug Stevenson
                Dec 20 '17 at 22:29






              • 1





                Does anyone know if they have plans to make this a feature? Especially with Cloud functions.firestore.document().field('myField/subFieldEtc').onUpdate()?

                – Jonathan002
                Jan 25 at 11:11
















              • 3





                Isn't that a big drawback compared to the firebase realtime database solution ?

                – httpete
                Dec 20 '17 at 22:19






              • 1





                Yes, this is known difference in behavior, and been discussed in various forums. If all you want to do is locate an existing key in the database and listen to changes, that's much easier in RTDB. In fact, there are a bunch of other known advantages (currently), documented at the end of this blog: firebase.googleblog.com/2017/10/…

                – Doug Stevenson
                Dec 20 '17 at 22:29






              • 1





                Does anyone know if they have plans to make this a feature? Especially with Cloud functions.firestore.document().field('myField/subFieldEtc').onUpdate()?

                – Jonathan002
                Jan 25 at 11:11










              3




              3





              Isn't that a big drawback compared to the firebase realtime database solution ?

              – httpete
              Dec 20 '17 at 22:19





              Isn't that a big drawback compared to the firebase realtime database solution ?

              – httpete
              Dec 20 '17 at 22:19




              1




              1





              Yes, this is known difference in behavior, and been discussed in various forums. If all you want to do is locate an existing key in the database and listen to changes, that's much easier in RTDB. In fact, there are a bunch of other known advantages (currently), documented at the end of this blog: firebase.googleblog.com/2017/10/…

              – Doug Stevenson
              Dec 20 '17 at 22:29





              Yes, this is known difference in behavior, and been discussed in various forums. If all you want to do is locate an existing key in the database and listen to changes, that's much easier in RTDB. In fact, there are a bunch of other known advantages (currently), documented at the end of this blog: firebase.googleblog.com/2017/10/…

              – Doug Stevenson
              Dec 20 '17 at 22:29




              1




              1





              Does anyone know if they have plans to make this a feature? Especially with Cloud functions.firestore.document().field('myField/subFieldEtc').onUpdate()?

              – Jonathan002
              Jan 25 at 11:11







              Does anyone know if they have plans to make this a feature? Especially with Cloud functions.firestore.document().field('myField/subFieldEtc').onUpdate()?

              – Jonathan002
              Jan 25 at 11:11















              1














              Just in case you want to ignore events in some fields, you can do something like:



              export const yourCloudFunction = functions.firestore
              .document('/your-path')
              .onUpdate(
              field('foo', 'REMOVED', (change, context) => {
              console.log('Will get here only if foo was removed');
              // ... Your implementation here
              }),
              );


              I created a gist of the field function here. Remember, it is not avoiding your function to run if changes happen in other fields, it will just not handle that over to you and return a resolved promise instead.






              share|improve this answer




























                1














                Just in case you want to ignore events in some fields, you can do something like:



                export const yourCloudFunction = functions.firestore
                .document('/your-path')
                .onUpdate(
                field('foo', 'REMOVED', (change, context) => {
                console.log('Will get here only if foo was removed');
                // ... Your implementation here
                }),
                );


                I created a gist of the field function here. Remember, it is not avoiding your function to run if changes happen in other fields, it will just not handle that over to you and return a resolved promise instead.






                share|improve this answer


























                  1












                  1








                  1







                  Just in case you want to ignore events in some fields, you can do something like:



                  export const yourCloudFunction = functions.firestore
                  .document('/your-path')
                  .onUpdate(
                  field('foo', 'REMOVED', (change, context) => {
                  console.log('Will get here only if foo was removed');
                  // ... Your implementation here
                  }),
                  );


                  I created a gist of the field function here. Remember, it is not avoiding your function to run if changes happen in other fields, it will just not handle that over to you and return a resolved promise instead.






                  share|improve this answer













                  Just in case you want to ignore events in some fields, you can do something like:



                  export const yourCloudFunction = functions.firestore
                  .document('/your-path')
                  .onUpdate(
                  field('foo', 'REMOVED', (change, context) => {
                  console.log('Will get here only if foo was removed');
                  // ... Your implementation here
                  }),
                  );


                  I created a gist of the field function here. Remember, it is not avoiding your function to run if changes happen in other fields, it will just not handle that over to you and return a resolved promise instead.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 26 '18 at 1:53









                  ChristianChristian

                  97112




                  97112























                      0














                      Listen for the document, then set a conditional on the field you're interesting in:



                      firebase.firestore().collection('Dictionaries').doc('Spanish').collection('Words').doc(word).collection('Pronunciations').doc('Castilian-female-IBM').onSnapshot(function(snapshot) {
                      if (snapshot.data().audioFiles) { // eliminates an error message
                      if (snapshot.data().audioFiles.length === 2) {
                      audioFilesReady++;
                      if (audioFilesReady === 3) {
                      $scope.showNextWord();
                      }
                      }
                      }
                      }, function(error) {
                      console.error(error);
                      });


                      I'm listening for a document for a voice (Castilian-female-IBM), which contains an array of audio files, in webm and mp3 formats. When both of those audio files have come back asynchronously then snapshot.data().audioFiles.length === 2. This increments a conditional. When two more voices come back (Castilian-male-IBM and Latin_American-female-IBM) then audioFilesReady === 3 and the next function $scope.showNextWord() fires.






                      share|improve this answer




























                        0














                        Listen for the document, then set a conditional on the field you're interesting in:



                        firebase.firestore().collection('Dictionaries').doc('Spanish').collection('Words').doc(word).collection('Pronunciations').doc('Castilian-female-IBM').onSnapshot(function(snapshot) {
                        if (snapshot.data().audioFiles) { // eliminates an error message
                        if (snapshot.data().audioFiles.length === 2) {
                        audioFilesReady++;
                        if (audioFilesReady === 3) {
                        $scope.showNextWord();
                        }
                        }
                        }
                        }, function(error) {
                        console.error(error);
                        });


                        I'm listening for a document for a voice (Castilian-female-IBM), which contains an array of audio files, in webm and mp3 formats. When both of those audio files have come back asynchronously then snapshot.data().audioFiles.length === 2. This increments a conditional. When two more voices come back (Castilian-male-IBM and Latin_American-female-IBM) then audioFilesReady === 3 and the next function $scope.showNextWord() fires.






                        share|improve this answer


























                          0












                          0








                          0







                          Listen for the document, then set a conditional on the field you're interesting in:



                          firebase.firestore().collection('Dictionaries').doc('Spanish').collection('Words').doc(word).collection('Pronunciations').doc('Castilian-female-IBM').onSnapshot(function(snapshot) {
                          if (snapshot.data().audioFiles) { // eliminates an error message
                          if (snapshot.data().audioFiles.length === 2) {
                          audioFilesReady++;
                          if (audioFilesReady === 3) {
                          $scope.showNextWord();
                          }
                          }
                          }
                          }, function(error) {
                          console.error(error);
                          });


                          I'm listening for a document for a voice (Castilian-female-IBM), which contains an array of audio files, in webm and mp3 formats. When both of those audio files have come back asynchronously then snapshot.data().audioFiles.length === 2. This increments a conditional. When two more voices come back (Castilian-male-IBM and Latin_American-female-IBM) then audioFilesReady === 3 and the next function $scope.showNextWord() fires.






                          share|improve this answer













                          Listen for the document, then set a conditional on the field you're interesting in:



                          firebase.firestore().collection('Dictionaries').doc('Spanish').collection('Words').doc(word).collection('Pronunciations').doc('Castilian-female-IBM').onSnapshot(function(snapshot) {
                          if (snapshot.data().audioFiles) { // eliminates an error message
                          if (snapshot.data().audioFiles.length === 2) {
                          audioFilesReady++;
                          if (audioFilesReady === 3) {
                          $scope.showNextWord();
                          }
                          }
                          }
                          }, function(error) {
                          console.error(error);
                          });


                          I'm listening for a document for a voice (Castilian-female-IBM), which contains an array of audio files, in webm and mp3 formats. When both of those audio files have come back asynchronously then snapshot.data().audioFiles.length === 2. This increments a conditional. When two more voices come back (Castilian-male-IBM and Latin_American-female-IBM) then audioFilesReady === 3 and the next function $scope.showNextWord() fires.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Feb 23 at 18:53









                          Thomas David KehoeThomas David Kehoe

                          2,25611437




                          2,25611437






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f47860531%2ffirestore-listen-to-specific-field-change%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Costa Masnaga

                              Fotorealismo

                              Create new schema in PostgreSQL using DBeaver