Return range of documents around ID in MongoDB





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







6















I have an ID of a document and need to return the document plus the 10 documents that come before and the 10 documents after it. 21 docs total.



I do not have a start or end value from any key. Only the limit in either direction.



Best way to do this? Thank you in advance.










share|improve this question

























  • Unfortunately I believe you would be stuck running a .find() to get the entire collection, iterating until you find your target doc, then getting the next +10/-10 - you need to have a cursor, which comes from .find()

    – tymeJV
    Feb 22 '14 at 0:10











  • @tymeJV I don't think so. See the answer

    – Neil Lunn
    Feb 22 '14 at 0:55











  • @NeilLunn -- well that's just awesome! +1

    – tymeJV
    Feb 22 '14 at 1:12


















6















I have an ID of a document and need to return the document plus the 10 documents that come before and the 10 documents after it. 21 docs total.



I do not have a start or end value from any key. Only the limit in either direction.



Best way to do this? Thank you in advance.










share|improve this question

























  • Unfortunately I believe you would be stuck running a .find() to get the entire collection, iterating until you find your target doc, then getting the next +10/-10 - you need to have a cursor, which comes from .find()

    – tymeJV
    Feb 22 '14 at 0:10











  • @tymeJV I don't think so. See the answer

    – Neil Lunn
    Feb 22 '14 at 0:55











  • @NeilLunn -- well that's just awesome! +1

    – tymeJV
    Feb 22 '14 at 1:12














6












6








6


2






I have an ID of a document and need to return the document plus the 10 documents that come before and the 10 documents after it. 21 docs total.



I do not have a start or end value from any key. Only the limit in either direction.



Best way to do this? Thank you in advance.










share|improve this question
















I have an ID of a document and need to return the document plus the 10 documents that come before and the 10 documents after it. 21 docs total.



I do not have a start or end value from any key. Only the limit in either direction.



Best way to do this? Thank you in advance.







mongodb mongodb-query






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jun 26 '17 at 10:07









Neil Lunn

101k23179187




101k23179187










asked Feb 22 '14 at 0:01









Brian MooreBrian Moore

72656




72656













  • Unfortunately I believe you would be stuck running a .find() to get the entire collection, iterating until you find your target doc, then getting the next +10/-10 - you need to have a cursor, which comes from .find()

    – tymeJV
    Feb 22 '14 at 0:10











  • @tymeJV I don't think so. See the answer

    – Neil Lunn
    Feb 22 '14 at 0:55











  • @NeilLunn -- well that's just awesome! +1

    – tymeJV
    Feb 22 '14 at 1:12



















  • Unfortunately I believe you would be stuck running a .find() to get the entire collection, iterating until you find your target doc, then getting the next +10/-10 - you need to have a cursor, which comes from .find()

    – tymeJV
    Feb 22 '14 at 0:10











  • @tymeJV I don't think so. See the answer

    – Neil Lunn
    Feb 22 '14 at 0:55











  • @NeilLunn -- well that's just awesome! +1

    – tymeJV
    Feb 22 '14 at 1:12

















Unfortunately I believe you would be stuck running a .find() to get the entire collection, iterating until you find your target doc, then getting the next +10/-10 - you need to have a cursor, which comes from .find()

– tymeJV
Feb 22 '14 at 0:10





Unfortunately I believe you would be stuck running a .find() to get the entire collection, iterating until you find your target doc, then getting the next +10/-10 - you need to have a cursor, which comes from .find()

– tymeJV
Feb 22 '14 at 0:10













@tymeJV I don't think so. See the answer

– Neil Lunn
Feb 22 '14 at 0:55





@tymeJV I don't think so. See the answer

– Neil Lunn
Feb 22 '14 at 0:55













@NeilLunn -- well that's just awesome! +1

– tymeJV
Feb 22 '14 at 1:12





@NeilLunn -- well that's just awesome! +1

– tymeJV
Feb 22 '14 at 1:12












2 Answers
2






active

oldest

votes


















6














Did you know that ObjectID's contain a timestamp? And that therefore they always represent the natural insertion order. So if you are looking for documents before an after a known document _id you can do this:



Our documents:



{ "_id" : ObjectId("5307f2d80f936e03d1a1d1c8"), "a" : 1 }
{ "_id" : ObjectId("5307f2db0f936e03d1a1d1c9"), "b" : 1 }
{ "_id" : ObjectId("5307f2de0f936e03d1a1d1ca"), "c" : 1 }
{ "_id" : ObjectId("5307f2e20f936e03d1a1d1cb"), "d" : 1 }
{ "_id" : ObjectId("5307f2e50f936e03d1a1d1cc"), "e" : 1 }
{ "_id" : ObjectId("5307f2e90f936e03d1a1d1cd"), "f" : 1 }
{ "_id" : ObjectId("5307f2ec0f936e03d1a1d1ce"), "g" : 1 }
{ "_id" : ObjectId("5307f2ee0f936e03d1a1d1cf"), "h" : 1 }
{ "_id" : ObjectId("5307f2f10f936e03d1a1d1d0"), "i" : 1 }
{ "_id" : ObjectId("5307f2f50f936e03d1a1d1d1"), "j" : 1 }
{ "_id" : ObjectId("5307f3020f936e03d1a1d1d2"), "j" : 1 }


So we know the _id of "f", get it and the next 2 documents:



> db.items.find({ _id: {$gte: ObjectId("5307f2e90f936e03d1a1d1cd") } }).limit(3)

{ "_id" : ObjectId("5307f2e90f936e03d1a1d1cd"), "f" : 1 }
{ "_id" : ObjectId("5307f2ec0f936e03d1a1d1ce"), "g" : 1 }
{ "_id" : ObjectId("5307f2ee0f936e03d1a1d1cf"), "h" : 1 }


And do the same in reverse:



> db.items.find({ _id: {$lte: ObjectId("5307f2e90f936e03d1a1d1cd") } })
.sort({ _id: -1 }).limit(3)
{ "_id" : ObjectId("5307f2e90f936e03d1a1d1cd"), "f" : 1 }
{ "_id" : ObjectId("5307f2e50f936e03d1a1d1cc"), "e" : 1 }
{ "_id" : ObjectId("5307f2e20f936e03d1a1d1cb"), "d" : 1 }


And that's a much better approach than scanning a collection.






share|improve this answer































    1














    Neil's answer is a good answer to the question as stated (assuming that you are using automatically generated ObjectIds), but keep in mind that there's some subtlety around the concept of the 10 documents before and after a given document.



    The complete format for an ObjectId is documented here. Note that it consists of the following fields:




    • timestamp to 1-second resolution,

    • machine identifier

    • process id

    • counter


    Generally if you don't specify your own _ids they are automatically generated by the driver on the client machine. So as long as the ObjectIds are generated on a single process on a client single machine, their order does indeed reflect the order in which they were generated, which in a typical application will also be the insertion order (but need not be). However if you have multiple processes or multiple client machines, the order of the ObjectIds for objects generated within a given second by those multiple sources has an unpredictable relationship to the insertion order.






    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%2f21947625%2freturn-range-of-documents-around-id-in-mongodb%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      6














      Did you know that ObjectID's contain a timestamp? And that therefore they always represent the natural insertion order. So if you are looking for documents before an after a known document _id you can do this:



      Our documents:



      { "_id" : ObjectId("5307f2d80f936e03d1a1d1c8"), "a" : 1 }
      { "_id" : ObjectId("5307f2db0f936e03d1a1d1c9"), "b" : 1 }
      { "_id" : ObjectId("5307f2de0f936e03d1a1d1ca"), "c" : 1 }
      { "_id" : ObjectId("5307f2e20f936e03d1a1d1cb"), "d" : 1 }
      { "_id" : ObjectId("5307f2e50f936e03d1a1d1cc"), "e" : 1 }
      { "_id" : ObjectId("5307f2e90f936e03d1a1d1cd"), "f" : 1 }
      { "_id" : ObjectId("5307f2ec0f936e03d1a1d1ce"), "g" : 1 }
      { "_id" : ObjectId("5307f2ee0f936e03d1a1d1cf"), "h" : 1 }
      { "_id" : ObjectId("5307f2f10f936e03d1a1d1d0"), "i" : 1 }
      { "_id" : ObjectId("5307f2f50f936e03d1a1d1d1"), "j" : 1 }
      { "_id" : ObjectId("5307f3020f936e03d1a1d1d2"), "j" : 1 }


      So we know the _id of "f", get it and the next 2 documents:



      > db.items.find({ _id: {$gte: ObjectId("5307f2e90f936e03d1a1d1cd") } }).limit(3)

      { "_id" : ObjectId("5307f2e90f936e03d1a1d1cd"), "f" : 1 }
      { "_id" : ObjectId("5307f2ec0f936e03d1a1d1ce"), "g" : 1 }
      { "_id" : ObjectId("5307f2ee0f936e03d1a1d1cf"), "h" : 1 }


      And do the same in reverse:



      > db.items.find({ _id: {$lte: ObjectId("5307f2e90f936e03d1a1d1cd") } })
      .sort({ _id: -1 }).limit(3)
      { "_id" : ObjectId("5307f2e90f936e03d1a1d1cd"), "f" : 1 }
      { "_id" : ObjectId("5307f2e50f936e03d1a1d1cc"), "e" : 1 }
      { "_id" : ObjectId("5307f2e20f936e03d1a1d1cb"), "d" : 1 }


      And that's a much better approach than scanning a collection.






      share|improve this answer




























        6














        Did you know that ObjectID's contain a timestamp? And that therefore they always represent the natural insertion order. So if you are looking for documents before an after a known document _id you can do this:



        Our documents:



        { "_id" : ObjectId("5307f2d80f936e03d1a1d1c8"), "a" : 1 }
        { "_id" : ObjectId("5307f2db0f936e03d1a1d1c9"), "b" : 1 }
        { "_id" : ObjectId("5307f2de0f936e03d1a1d1ca"), "c" : 1 }
        { "_id" : ObjectId("5307f2e20f936e03d1a1d1cb"), "d" : 1 }
        { "_id" : ObjectId("5307f2e50f936e03d1a1d1cc"), "e" : 1 }
        { "_id" : ObjectId("5307f2e90f936e03d1a1d1cd"), "f" : 1 }
        { "_id" : ObjectId("5307f2ec0f936e03d1a1d1ce"), "g" : 1 }
        { "_id" : ObjectId("5307f2ee0f936e03d1a1d1cf"), "h" : 1 }
        { "_id" : ObjectId("5307f2f10f936e03d1a1d1d0"), "i" : 1 }
        { "_id" : ObjectId("5307f2f50f936e03d1a1d1d1"), "j" : 1 }
        { "_id" : ObjectId("5307f3020f936e03d1a1d1d2"), "j" : 1 }


        So we know the _id of "f", get it and the next 2 documents:



        > db.items.find({ _id: {$gte: ObjectId("5307f2e90f936e03d1a1d1cd") } }).limit(3)

        { "_id" : ObjectId("5307f2e90f936e03d1a1d1cd"), "f" : 1 }
        { "_id" : ObjectId("5307f2ec0f936e03d1a1d1ce"), "g" : 1 }
        { "_id" : ObjectId("5307f2ee0f936e03d1a1d1cf"), "h" : 1 }


        And do the same in reverse:



        > db.items.find({ _id: {$lte: ObjectId("5307f2e90f936e03d1a1d1cd") } })
        .sort({ _id: -1 }).limit(3)
        { "_id" : ObjectId("5307f2e90f936e03d1a1d1cd"), "f" : 1 }
        { "_id" : ObjectId("5307f2e50f936e03d1a1d1cc"), "e" : 1 }
        { "_id" : ObjectId("5307f2e20f936e03d1a1d1cb"), "d" : 1 }


        And that's a much better approach than scanning a collection.






        share|improve this answer


























          6












          6








          6







          Did you know that ObjectID's contain a timestamp? And that therefore they always represent the natural insertion order. So if you are looking for documents before an after a known document _id you can do this:



          Our documents:



          { "_id" : ObjectId("5307f2d80f936e03d1a1d1c8"), "a" : 1 }
          { "_id" : ObjectId("5307f2db0f936e03d1a1d1c9"), "b" : 1 }
          { "_id" : ObjectId("5307f2de0f936e03d1a1d1ca"), "c" : 1 }
          { "_id" : ObjectId("5307f2e20f936e03d1a1d1cb"), "d" : 1 }
          { "_id" : ObjectId("5307f2e50f936e03d1a1d1cc"), "e" : 1 }
          { "_id" : ObjectId("5307f2e90f936e03d1a1d1cd"), "f" : 1 }
          { "_id" : ObjectId("5307f2ec0f936e03d1a1d1ce"), "g" : 1 }
          { "_id" : ObjectId("5307f2ee0f936e03d1a1d1cf"), "h" : 1 }
          { "_id" : ObjectId("5307f2f10f936e03d1a1d1d0"), "i" : 1 }
          { "_id" : ObjectId("5307f2f50f936e03d1a1d1d1"), "j" : 1 }
          { "_id" : ObjectId("5307f3020f936e03d1a1d1d2"), "j" : 1 }


          So we know the _id of "f", get it and the next 2 documents:



          > db.items.find({ _id: {$gte: ObjectId("5307f2e90f936e03d1a1d1cd") } }).limit(3)

          { "_id" : ObjectId("5307f2e90f936e03d1a1d1cd"), "f" : 1 }
          { "_id" : ObjectId("5307f2ec0f936e03d1a1d1ce"), "g" : 1 }
          { "_id" : ObjectId("5307f2ee0f936e03d1a1d1cf"), "h" : 1 }


          And do the same in reverse:



          > db.items.find({ _id: {$lte: ObjectId("5307f2e90f936e03d1a1d1cd") } })
          .sort({ _id: -1 }).limit(3)
          { "_id" : ObjectId("5307f2e90f936e03d1a1d1cd"), "f" : 1 }
          { "_id" : ObjectId("5307f2e50f936e03d1a1d1cc"), "e" : 1 }
          { "_id" : ObjectId("5307f2e20f936e03d1a1d1cb"), "d" : 1 }


          And that's a much better approach than scanning a collection.






          share|improve this answer













          Did you know that ObjectID's contain a timestamp? And that therefore they always represent the natural insertion order. So if you are looking for documents before an after a known document _id you can do this:



          Our documents:



          { "_id" : ObjectId("5307f2d80f936e03d1a1d1c8"), "a" : 1 }
          { "_id" : ObjectId("5307f2db0f936e03d1a1d1c9"), "b" : 1 }
          { "_id" : ObjectId("5307f2de0f936e03d1a1d1ca"), "c" : 1 }
          { "_id" : ObjectId("5307f2e20f936e03d1a1d1cb"), "d" : 1 }
          { "_id" : ObjectId("5307f2e50f936e03d1a1d1cc"), "e" : 1 }
          { "_id" : ObjectId("5307f2e90f936e03d1a1d1cd"), "f" : 1 }
          { "_id" : ObjectId("5307f2ec0f936e03d1a1d1ce"), "g" : 1 }
          { "_id" : ObjectId("5307f2ee0f936e03d1a1d1cf"), "h" : 1 }
          { "_id" : ObjectId("5307f2f10f936e03d1a1d1d0"), "i" : 1 }
          { "_id" : ObjectId("5307f2f50f936e03d1a1d1d1"), "j" : 1 }
          { "_id" : ObjectId("5307f3020f936e03d1a1d1d2"), "j" : 1 }


          So we know the _id of "f", get it and the next 2 documents:



          > db.items.find({ _id: {$gte: ObjectId("5307f2e90f936e03d1a1d1cd") } }).limit(3)

          { "_id" : ObjectId("5307f2e90f936e03d1a1d1cd"), "f" : 1 }
          { "_id" : ObjectId("5307f2ec0f936e03d1a1d1ce"), "g" : 1 }
          { "_id" : ObjectId("5307f2ee0f936e03d1a1d1cf"), "h" : 1 }


          And do the same in reverse:



          > db.items.find({ _id: {$lte: ObjectId("5307f2e90f936e03d1a1d1cd") } })
          .sort({ _id: -1 }).limit(3)
          { "_id" : ObjectId("5307f2e90f936e03d1a1d1cd"), "f" : 1 }
          { "_id" : ObjectId("5307f2e50f936e03d1a1d1cc"), "e" : 1 }
          { "_id" : ObjectId("5307f2e20f936e03d1a1d1cb"), "d" : 1 }


          And that's a much better approach than scanning a collection.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Feb 22 '14 at 0:55









          Neil LunnNeil Lunn

          101k23179187




          101k23179187

























              1














              Neil's answer is a good answer to the question as stated (assuming that you are using automatically generated ObjectIds), but keep in mind that there's some subtlety around the concept of the 10 documents before and after a given document.



              The complete format for an ObjectId is documented here. Note that it consists of the following fields:




              • timestamp to 1-second resolution,

              • machine identifier

              • process id

              • counter


              Generally if you don't specify your own _ids they are automatically generated by the driver on the client machine. So as long as the ObjectIds are generated on a single process on a client single machine, their order does indeed reflect the order in which they were generated, which in a typical application will also be the insertion order (but need not be). However if you have multiple processes or multiple client machines, the order of the ObjectIds for objects generated within a given second by those multiple sources has an unpredictable relationship to the insertion order.






              share|improve this answer




























                1














                Neil's answer is a good answer to the question as stated (assuming that you are using automatically generated ObjectIds), but keep in mind that there's some subtlety around the concept of the 10 documents before and after a given document.



                The complete format for an ObjectId is documented here. Note that it consists of the following fields:




                • timestamp to 1-second resolution,

                • machine identifier

                • process id

                • counter


                Generally if you don't specify your own _ids they are automatically generated by the driver on the client machine. So as long as the ObjectIds are generated on a single process on a client single machine, their order does indeed reflect the order in which they were generated, which in a typical application will also be the insertion order (but need not be). However if you have multiple processes or multiple client machines, the order of the ObjectIds for objects generated within a given second by those multiple sources has an unpredictable relationship to the insertion order.






                share|improve this answer


























                  1












                  1








                  1







                  Neil's answer is a good answer to the question as stated (assuming that you are using automatically generated ObjectIds), but keep in mind that there's some subtlety around the concept of the 10 documents before and after a given document.



                  The complete format for an ObjectId is documented here. Note that it consists of the following fields:




                  • timestamp to 1-second resolution,

                  • machine identifier

                  • process id

                  • counter


                  Generally if you don't specify your own _ids they are automatically generated by the driver on the client machine. So as long as the ObjectIds are generated on a single process on a client single machine, their order does indeed reflect the order in which they were generated, which in a typical application will also be the insertion order (but need not be). However if you have multiple processes or multiple client machines, the order of the ObjectIds for objects generated within a given second by those multiple sources has an unpredictable relationship to the insertion order.






                  share|improve this answer













                  Neil's answer is a good answer to the question as stated (assuming that you are using automatically generated ObjectIds), but keep in mind that there's some subtlety around the concept of the 10 documents before and after a given document.



                  The complete format for an ObjectId is documented here. Note that it consists of the following fields:




                  • timestamp to 1-second resolution,

                  • machine identifier

                  • process id

                  • counter


                  Generally if you don't specify your own _ids they are automatically generated by the driver on the client machine. So as long as the ObjectIds are generated on a single process on a client single machine, their order does indeed reflect the order in which they were generated, which in a typical application will also be the insertion order (but need not be). However if you have multiple processes or multiple client machines, the order of the ObjectIds for objects generated within a given second by those multiple sources has an unpredictable relationship to the insertion order.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Feb 27 '14 at 20:58









                  Bruce LucasBruce Lucas

                  88955




                  88955






























                      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%2f21947625%2freturn-range-of-documents-around-id-in-mongodb%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

                      Guess what letter conforming each word

                      Port of Spain

                      Run scheduled task as local user group (not BUILTIN)