Order by on missing field constrains result










2















I'm running into an issue with Firestore that is pretty unintuitive to me and so I'm wondering if anyone can help me understand why Firestore is giving this result.



Let's say I have a collection called "people" with fields "firstName" and "lastName". Let's say I have 20 documents in that collection that have the lastName "Quaid". I then have a field "inCanada" that is only present in a subset of those 20 documents, let's say 5. The other 15 documents don't have that field.



What is surprising is that an orderBy clause using that optional field is actually filtering the result set rather than just sorting it, which doesn't make sense to me when compared with other databases.



In the below example, I expected both result sets to have 20 elements, however the second set that has the orderBy only has those 5 documents where that field is present.



Could someone please explain why Firestore does this?



const Admin = require('firebase-admin');

(async function ()
const initFirebase = require('../initFirebase');
initFirebase();

const people = Admin.firestore().collection('people');
const quaids = people.where('lastName', '==', 'Quaid')
const quaids1 = await quaids.get();
const quaids2 = await quaids.orderBy('inCanada', 'desc').get();

console.log(quaids1._size); // 20
console.log(quaids2._size); // 5
)();









share|improve this question


























    2















    I'm running into an issue with Firestore that is pretty unintuitive to me and so I'm wondering if anyone can help me understand why Firestore is giving this result.



    Let's say I have a collection called "people" with fields "firstName" and "lastName". Let's say I have 20 documents in that collection that have the lastName "Quaid". I then have a field "inCanada" that is only present in a subset of those 20 documents, let's say 5. The other 15 documents don't have that field.



    What is surprising is that an orderBy clause using that optional field is actually filtering the result set rather than just sorting it, which doesn't make sense to me when compared with other databases.



    In the below example, I expected both result sets to have 20 elements, however the second set that has the orderBy only has those 5 documents where that field is present.



    Could someone please explain why Firestore does this?



    const Admin = require('firebase-admin');

    (async function ()
    const initFirebase = require('../initFirebase');
    initFirebase();

    const people = Admin.firestore().collection('people');
    const quaids = people.where('lastName', '==', 'Quaid')
    const quaids1 = await quaids.get();
    const quaids2 = await quaids.orderBy('inCanada', 'desc').get();

    console.log(quaids1._size); // 20
    console.log(quaids2._size); // 5
    )();









    share|improve this question
























      2












      2








      2








      I'm running into an issue with Firestore that is pretty unintuitive to me and so I'm wondering if anyone can help me understand why Firestore is giving this result.



      Let's say I have a collection called "people" with fields "firstName" and "lastName". Let's say I have 20 documents in that collection that have the lastName "Quaid". I then have a field "inCanada" that is only present in a subset of those 20 documents, let's say 5. The other 15 documents don't have that field.



      What is surprising is that an orderBy clause using that optional field is actually filtering the result set rather than just sorting it, which doesn't make sense to me when compared with other databases.



      In the below example, I expected both result sets to have 20 elements, however the second set that has the orderBy only has those 5 documents where that field is present.



      Could someone please explain why Firestore does this?



      const Admin = require('firebase-admin');

      (async function ()
      const initFirebase = require('../initFirebase');
      initFirebase();

      const people = Admin.firestore().collection('people');
      const quaids = people.where('lastName', '==', 'Quaid')
      const quaids1 = await quaids.get();
      const quaids2 = await quaids.orderBy('inCanada', 'desc').get();

      console.log(quaids1._size); // 20
      console.log(quaids2._size); // 5
      )();









      share|improve this question














      I'm running into an issue with Firestore that is pretty unintuitive to me and so I'm wondering if anyone can help me understand why Firestore is giving this result.



      Let's say I have a collection called "people" with fields "firstName" and "lastName". Let's say I have 20 documents in that collection that have the lastName "Quaid". I then have a field "inCanada" that is only present in a subset of those 20 documents, let's say 5. The other 15 documents don't have that field.



      What is surprising is that an orderBy clause using that optional field is actually filtering the result set rather than just sorting it, which doesn't make sense to me when compared with other databases.



      In the below example, I expected both result sets to have 20 elements, however the second set that has the orderBy only has those 5 documents where that field is present.



      Could someone please explain why Firestore does this?



      const Admin = require('firebase-admin');

      (async function ()
      const initFirebase = require('../initFirebase');
      initFirebase();

      const people = Admin.firestore().collection('people');
      const quaids = people.where('lastName', '==', 'Quaid')
      const quaids1 = await quaids.get();
      const quaids2 = await quaids.orderBy('inCanada', 'desc').get();

      console.log(quaids1._size); // 20
      console.log(quaids2._size); // 5
      )();






      firebase google-cloud-firestore






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 16 '18 at 1:12









      Stephen HandleyStephen Handley

      1,07121214




      1,07121214






















          1 Answer
          1






          active

          oldest

          votes


















          2














          In a nutshell, this is because Firestore queries are based on indexes: each time you create a doc Firestore creates an index for each field of the doc.



          So since the field "inCanada" is only present "in a subset of the 20 documents", only this subset of documents is present in the "inCanada" index, resulting in the situation you mention.



          This official video from Todd Kerpelman from the Firebase team explains it very well: https://www.youtube.com/watch?v=Ofux_4c94FI#t=4m17s. (This link will open the video at 4m17s i.e. when the explanations on the index mechanism start. However the part on the impact of indexes on the querying possibilities is more around 6m22s!)



          If you want to include the other documents in your query results you should write a value for the field "inCanada" for those documents, for example use a false value (and use true for the docs that are "inCanada").






          share|improve this answer




















          • 1





            Hi Renaud! I think it will be helpful for the OP to add the specific min at which this explanation take place. You can add something like this #t=6m22s at the end of the url.

            – Alex Mamo
            Nov 16 '18 at 10:30






          • 1





            @AlexMamo Hi Alex, good suggestion, thanks! I've adapted the answer.

            – Renaud Tarnec
            Nov 16 '18 at 14:16












          • You're welcome Renaud!

            – Alex Mamo
            Nov 16 '18 at 14:17






          • 1





            Yeah I'm aware of how indexing works, my question was more of a why than a how. Just seems like a really awkward design decision to have an orderBy affect the size of a result set that doesn't have any limit specified on it. Anyway, thanks for answering.

            – Stephen Handley
            Nov 16 '18 at 17:36






          • 1





            I've got an answer from the firebase support : this is indeed not supported at this time due the way querying use the indexes, but they created a feature request for this. So this MAY BE will be supported in the future.

            – Christophe Le Besnerais
            Jan 2 at 14:50










          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%2f53330078%2forder-by-on-missing-field-constrains-result%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          In a nutshell, this is because Firestore queries are based on indexes: each time you create a doc Firestore creates an index for each field of the doc.



          So since the field "inCanada" is only present "in a subset of the 20 documents", only this subset of documents is present in the "inCanada" index, resulting in the situation you mention.



          This official video from Todd Kerpelman from the Firebase team explains it very well: https://www.youtube.com/watch?v=Ofux_4c94FI#t=4m17s. (This link will open the video at 4m17s i.e. when the explanations on the index mechanism start. However the part on the impact of indexes on the querying possibilities is more around 6m22s!)



          If you want to include the other documents in your query results you should write a value for the field "inCanada" for those documents, for example use a false value (and use true for the docs that are "inCanada").






          share|improve this answer




















          • 1





            Hi Renaud! I think it will be helpful for the OP to add the specific min at which this explanation take place. You can add something like this #t=6m22s at the end of the url.

            – Alex Mamo
            Nov 16 '18 at 10:30






          • 1





            @AlexMamo Hi Alex, good suggestion, thanks! I've adapted the answer.

            – Renaud Tarnec
            Nov 16 '18 at 14:16












          • You're welcome Renaud!

            – Alex Mamo
            Nov 16 '18 at 14:17






          • 1





            Yeah I'm aware of how indexing works, my question was more of a why than a how. Just seems like a really awkward design decision to have an orderBy affect the size of a result set that doesn't have any limit specified on it. Anyway, thanks for answering.

            – Stephen Handley
            Nov 16 '18 at 17:36






          • 1





            I've got an answer from the firebase support : this is indeed not supported at this time due the way querying use the indexes, but they created a feature request for this. So this MAY BE will be supported in the future.

            – Christophe Le Besnerais
            Jan 2 at 14:50















          2














          In a nutshell, this is because Firestore queries are based on indexes: each time you create a doc Firestore creates an index for each field of the doc.



          So since the field "inCanada" is only present "in a subset of the 20 documents", only this subset of documents is present in the "inCanada" index, resulting in the situation you mention.



          This official video from Todd Kerpelman from the Firebase team explains it very well: https://www.youtube.com/watch?v=Ofux_4c94FI#t=4m17s. (This link will open the video at 4m17s i.e. when the explanations on the index mechanism start. However the part on the impact of indexes on the querying possibilities is more around 6m22s!)



          If you want to include the other documents in your query results you should write a value for the field "inCanada" for those documents, for example use a false value (and use true for the docs that are "inCanada").






          share|improve this answer




















          • 1





            Hi Renaud! I think it will be helpful for the OP to add the specific min at which this explanation take place. You can add something like this #t=6m22s at the end of the url.

            – Alex Mamo
            Nov 16 '18 at 10:30






          • 1





            @AlexMamo Hi Alex, good suggestion, thanks! I've adapted the answer.

            – Renaud Tarnec
            Nov 16 '18 at 14:16












          • You're welcome Renaud!

            – Alex Mamo
            Nov 16 '18 at 14:17






          • 1





            Yeah I'm aware of how indexing works, my question was more of a why than a how. Just seems like a really awkward design decision to have an orderBy affect the size of a result set that doesn't have any limit specified on it. Anyway, thanks for answering.

            – Stephen Handley
            Nov 16 '18 at 17:36






          • 1





            I've got an answer from the firebase support : this is indeed not supported at this time due the way querying use the indexes, but they created a feature request for this. So this MAY BE will be supported in the future.

            – Christophe Le Besnerais
            Jan 2 at 14:50













          2












          2








          2







          In a nutshell, this is because Firestore queries are based on indexes: each time you create a doc Firestore creates an index for each field of the doc.



          So since the field "inCanada" is only present "in a subset of the 20 documents", only this subset of documents is present in the "inCanada" index, resulting in the situation you mention.



          This official video from Todd Kerpelman from the Firebase team explains it very well: https://www.youtube.com/watch?v=Ofux_4c94FI#t=4m17s. (This link will open the video at 4m17s i.e. when the explanations on the index mechanism start. However the part on the impact of indexes on the querying possibilities is more around 6m22s!)



          If you want to include the other documents in your query results you should write a value for the field "inCanada" for those documents, for example use a false value (and use true for the docs that are "inCanada").






          share|improve this answer















          In a nutshell, this is because Firestore queries are based on indexes: each time you create a doc Firestore creates an index for each field of the doc.



          So since the field "inCanada" is only present "in a subset of the 20 documents", only this subset of documents is present in the "inCanada" index, resulting in the situation you mention.



          This official video from Todd Kerpelman from the Firebase team explains it very well: https://www.youtube.com/watch?v=Ofux_4c94FI#t=4m17s. (This link will open the video at 4m17s i.e. when the explanations on the index mechanism start. However the part on the impact of indexes on the querying possibilities is more around 6m22s!)



          If you want to include the other documents in your query results you should write a value for the field "inCanada" for those documents, for example use a false value (and use true for the docs that are "inCanada").







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 16 '18 at 14:15

























          answered Nov 16 '18 at 8:46









          Renaud TarnecRenaud Tarnec

          12.3k21734




          12.3k21734







          • 1





            Hi Renaud! I think it will be helpful for the OP to add the specific min at which this explanation take place. You can add something like this #t=6m22s at the end of the url.

            – Alex Mamo
            Nov 16 '18 at 10:30






          • 1





            @AlexMamo Hi Alex, good suggestion, thanks! I've adapted the answer.

            – Renaud Tarnec
            Nov 16 '18 at 14:16












          • You're welcome Renaud!

            – Alex Mamo
            Nov 16 '18 at 14:17






          • 1





            Yeah I'm aware of how indexing works, my question was more of a why than a how. Just seems like a really awkward design decision to have an orderBy affect the size of a result set that doesn't have any limit specified on it. Anyway, thanks for answering.

            – Stephen Handley
            Nov 16 '18 at 17:36






          • 1





            I've got an answer from the firebase support : this is indeed not supported at this time due the way querying use the indexes, but they created a feature request for this. So this MAY BE will be supported in the future.

            – Christophe Le Besnerais
            Jan 2 at 14:50












          • 1





            Hi Renaud! I think it will be helpful for the OP to add the specific min at which this explanation take place. You can add something like this #t=6m22s at the end of the url.

            – Alex Mamo
            Nov 16 '18 at 10:30






          • 1





            @AlexMamo Hi Alex, good suggestion, thanks! I've adapted the answer.

            – Renaud Tarnec
            Nov 16 '18 at 14:16












          • You're welcome Renaud!

            – Alex Mamo
            Nov 16 '18 at 14:17






          • 1





            Yeah I'm aware of how indexing works, my question was more of a why than a how. Just seems like a really awkward design decision to have an orderBy affect the size of a result set that doesn't have any limit specified on it. Anyway, thanks for answering.

            – Stephen Handley
            Nov 16 '18 at 17:36






          • 1





            I've got an answer from the firebase support : this is indeed not supported at this time due the way querying use the indexes, but they created a feature request for this. So this MAY BE will be supported in the future.

            – Christophe Le Besnerais
            Jan 2 at 14:50







          1




          1





          Hi Renaud! I think it will be helpful for the OP to add the specific min at which this explanation take place. You can add something like this #t=6m22s at the end of the url.

          – Alex Mamo
          Nov 16 '18 at 10:30





          Hi Renaud! I think it will be helpful for the OP to add the specific min at which this explanation take place. You can add something like this #t=6m22s at the end of the url.

          – Alex Mamo
          Nov 16 '18 at 10:30




          1




          1





          @AlexMamo Hi Alex, good suggestion, thanks! I've adapted the answer.

          – Renaud Tarnec
          Nov 16 '18 at 14:16






          @AlexMamo Hi Alex, good suggestion, thanks! I've adapted the answer.

          – Renaud Tarnec
          Nov 16 '18 at 14:16














          You're welcome Renaud!

          – Alex Mamo
          Nov 16 '18 at 14:17





          You're welcome Renaud!

          – Alex Mamo
          Nov 16 '18 at 14:17




          1




          1





          Yeah I'm aware of how indexing works, my question was more of a why than a how. Just seems like a really awkward design decision to have an orderBy affect the size of a result set that doesn't have any limit specified on it. Anyway, thanks for answering.

          – Stephen Handley
          Nov 16 '18 at 17:36





          Yeah I'm aware of how indexing works, my question was more of a why than a how. Just seems like a really awkward design decision to have an orderBy affect the size of a result set that doesn't have any limit specified on it. Anyway, thanks for answering.

          – Stephen Handley
          Nov 16 '18 at 17:36




          1




          1





          I've got an answer from the firebase support : this is indeed not supported at this time due the way querying use the indexes, but they created a feature request for this. So this MAY BE will be supported in the future.

          – Christophe Le Besnerais
          Jan 2 at 14:50





          I've got an answer from the firebase support : this is indeed not supported at this time due the way querying use the indexes, but they created a feature request for this. So this MAY BE will be supported in the future.

          – Christophe Le Besnerais
          Jan 2 at 14:50



















          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%2f53330078%2forder-by-on-missing-field-constrains-result%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

          Top Tejano songwriter Luis Silva dead of heart attack at 64

          政党

          天津地下鉄3号線