How to delete N numbers of documents in mongodb












34















In my collections, documents contains key like status and timestamp. When I want to find latest ten documents then I write following query



db.collectionsname.find().sort({"timestamp"-1}).limit(10)


This query gives me results which I want but when I want to delete latest ten documents then I was writing the following query



db.collectionsname.remove({"status":0},10).sort({"timestamp":-1})


but it shows following error
TypeError: Cannot call method 'sort' of undefined
and again I wrote the same query as below
db.collectionsname.remove({"status":0},10)
It deletes only one document. So how can I write a query which deletes ten latest documents and sorts on timestamp?










share|improve this question




















  • 1





    That might help you docs.mongodb.org/manual/reference/command/findAndModify

    – Mina
    Sep 28 '13 at 10:03
















34















In my collections, documents contains key like status and timestamp. When I want to find latest ten documents then I write following query



db.collectionsname.find().sort({"timestamp"-1}).limit(10)


This query gives me results which I want but when I want to delete latest ten documents then I was writing the following query



db.collectionsname.remove({"status":0},10).sort({"timestamp":-1})


but it shows following error
TypeError: Cannot call method 'sort' of undefined
and again I wrote the same query as below
db.collectionsname.remove({"status":0},10)
It deletes only one document. So how can I write a query which deletes ten latest documents and sorts on timestamp?










share|improve this question




















  • 1





    That might help you docs.mongodb.org/manual/reference/command/findAndModify

    – Mina
    Sep 28 '13 at 10:03














34












34








34


16






In my collections, documents contains key like status and timestamp. When I want to find latest ten documents then I write following query



db.collectionsname.find().sort({"timestamp"-1}).limit(10)


This query gives me results which I want but when I want to delete latest ten documents then I was writing the following query



db.collectionsname.remove({"status":0},10).sort({"timestamp":-1})


but it shows following error
TypeError: Cannot call method 'sort' of undefined
and again I wrote the same query as below
db.collectionsname.remove({"status":0},10)
It deletes only one document. So how can I write a query which deletes ten latest documents and sorts on timestamp?










share|improve this question
















In my collections, documents contains key like status and timestamp. When I want to find latest ten documents then I write following query



db.collectionsname.find().sort({"timestamp"-1}).limit(10)


This query gives me results which I want but when I want to delete latest ten documents then I was writing the following query



db.collectionsname.remove({"status":0},10).sort({"timestamp":-1})


but it shows following error
TypeError: Cannot call method 'sort' of undefined
and again I wrote the same query as below
db.collectionsname.remove({"status":0},10)
It deletes only one document. So how can I write a query which deletes ten latest documents and sorts on timestamp?







mongodb






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 31 '16 at 14:02









Shabbir Essaji

111110




111110










asked Sep 28 '13 at 9:53









YogeshYogesh

5,63432144




5,63432144








  • 1





    That might help you docs.mongodb.org/manual/reference/command/findAndModify

    – Mina
    Sep 28 '13 at 10:03














  • 1





    That might help you docs.mongodb.org/manual/reference/command/findAndModify

    – Mina
    Sep 28 '13 at 10:03








1




1





That might help you docs.mongodb.org/manual/reference/command/findAndModify

– Mina
Sep 28 '13 at 10:03





That might help you docs.mongodb.org/manual/reference/command/findAndModify

– Mina
Sep 28 '13 at 10:03












4 Answers
4






active

oldest

votes


















63














You can't set a limit when using remove or findAndModify. So, if you want to precisely limit the number of documents removed, you'll need to do it in two steps.



db.collectionName.find({}, {_id : 1})
.limit(100)
.sort({timestamp:-1})
.toArray()
.map(function(doc) { return doc._id; }); // Pull out just the _ids


Then pass the returned _ids to the remove method:



db.collectionName.remove({_id: {$in: removeIdsArray}})


FYI: you cannot remove documents from a capped collection.






share|improve this answer


























  • Who do you have to do the sort part if all the records are going to be deleted?

    – ULazdins
    Jul 22 '14 at 12:52






  • 2





    Correct me if I'm wrong, but it makes difference whether limit(100) comes before sort({timestamp:-1}) or after. In the example @WiredPrairie limits results to 100 and sorts them afterwards (.sort({timestamp:-1}).limit(100)). In this case the sorting statement has no effect on the records returned. On the other hand, if you would write .limit(100).sort({timestamp:-1}), it would make a difference.

    – ULazdins
    Oct 14 '15 at 14:46








  • 10





    @ULazdins "Mongo applies the sort before limiting the results regardless of the order you call sort and limit on the cursor." (source)

    – joeytwiddle
    Apr 5 '16 at 3:37











  • @Madbreaks Afforementioned "new" answer is wrong. Read the comment under that.

    – Nuri Tasdemir
    May 31 '17 at 14:31











  • what if the numbers is large? I got the error : "Converting from JavaScript to BSON failed: Object size 19888938 exceeds limit of 16793600 bytes. "

    – Jaskey
    Sep 13 '17 at 6:14



















0














Another way is to write a python script.



from pymongo import MongoClient

def main():
local_client = MongoClient()
collection = local_client.database.collection
cursor = collection.find()
total_number_of_records = 10000

for document in cursor:
id = document.get("_id")

if total_number_of_records == 100:
break

delete_query = {"_id": id}
collection.delete_one(delete_query)

total_number_of_records -= 1

if __name__ == "__main__":
# execute only if run as a script
main()





share|improve this answer

































    0














    Let N be number of records to delete.



        db.collectionName.find().limit(N).forEach(doc => 
    {
    db.collectionName.remove({_id:doc._id})
    }
    )





    share|improve this answer































      -5














      Below query will find and delete the latest 10 documents from collection:-



      db.collectionsname.findAndModify({
      query: { 'status':0 },
      sort: { 'timestamp': -1 },
      limit: 10,
      remove: true
      });





      share|improve this answer





















      • 5





        docs.mongodb.com/manual/reference/method/… This doesn't support a "limit" field, and in fact if you try it, you will find it only deletes 1 element (as that is the default logic)

        – tweak2
        May 15 '17 at 16:33











      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%2f19065615%2fhow-to-delete-n-numbers-of-documents-in-mongodb%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      63














      You can't set a limit when using remove or findAndModify. So, if you want to precisely limit the number of documents removed, you'll need to do it in two steps.



      db.collectionName.find({}, {_id : 1})
      .limit(100)
      .sort({timestamp:-1})
      .toArray()
      .map(function(doc) { return doc._id; }); // Pull out just the _ids


      Then pass the returned _ids to the remove method:



      db.collectionName.remove({_id: {$in: removeIdsArray}})


      FYI: you cannot remove documents from a capped collection.






      share|improve this answer


























      • Who do you have to do the sort part if all the records are going to be deleted?

        – ULazdins
        Jul 22 '14 at 12:52






      • 2





        Correct me if I'm wrong, but it makes difference whether limit(100) comes before sort({timestamp:-1}) or after. In the example @WiredPrairie limits results to 100 and sorts them afterwards (.sort({timestamp:-1}).limit(100)). In this case the sorting statement has no effect on the records returned. On the other hand, if you would write .limit(100).sort({timestamp:-1}), it would make a difference.

        – ULazdins
        Oct 14 '15 at 14:46








      • 10





        @ULazdins "Mongo applies the sort before limiting the results regardless of the order you call sort and limit on the cursor." (source)

        – joeytwiddle
        Apr 5 '16 at 3:37











      • @Madbreaks Afforementioned "new" answer is wrong. Read the comment under that.

        – Nuri Tasdemir
        May 31 '17 at 14:31











      • what if the numbers is large? I got the error : "Converting from JavaScript to BSON failed: Object size 19888938 exceeds limit of 16793600 bytes. "

        – Jaskey
        Sep 13 '17 at 6:14
















      63














      You can't set a limit when using remove or findAndModify. So, if you want to precisely limit the number of documents removed, you'll need to do it in two steps.



      db.collectionName.find({}, {_id : 1})
      .limit(100)
      .sort({timestamp:-1})
      .toArray()
      .map(function(doc) { return doc._id; }); // Pull out just the _ids


      Then pass the returned _ids to the remove method:



      db.collectionName.remove({_id: {$in: removeIdsArray}})


      FYI: you cannot remove documents from a capped collection.






      share|improve this answer


























      • Who do you have to do the sort part if all the records are going to be deleted?

        – ULazdins
        Jul 22 '14 at 12:52






      • 2





        Correct me if I'm wrong, but it makes difference whether limit(100) comes before sort({timestamp:-1}) or after. In the example @WiredPrairie limits results to 100 and sorts them afterwards (.sort({timestamp:-1}).limit(100)). In this case the sorting statement has no effect on the records returned. On the other hand, if you would write .limit(100).sort({timestamp:-1}), it would make a difference.

        – ULazdins
        Oct 14 '15 at 14:46








      • 10





        @ULazdins "Mongo applies the sort before limiting the results regardless of the order you call sort and limit on the cursor." (source)

        – joeytwiddle
        Apr 5 '16 at 3:37











      • @Madbreaks Afforementioned "new" answer is wrong. Read the comment under that.

        – Nuri Tasdemir
        May 31 '17 at 14:31











      • what if the numbers is large? I got the error : "Converting from JavaScript to BSON failed: Object size 19888938 exceeds limit of 16793600 bytes. "

        – Jaskey
        Sep 13 '17 at 6:14














      63












      63








      63







      You can't set a limit when using remove or findAndModify. So, if you want to precisely limit the number of documents removed, you'll need to do it in two steps.



      db.collectionName.find({}, {_id : 1})
      .limit(100)
      .sort({timestamp:-1})
      .toArray()
      .map(function(doc) { return doc._id; }); // Pull out just the _ids


      Then pass the returned _ids to the remove method:



      db.collectionName.remove({_id: {$in: removeIdsArray}})


      FYI: you cannot remove documents from a capped collection.






      share|improve this answer















      You can't set a limit when using remove or findAndModify. So, if you want to precisely limit the number of documents removed, you'll need to do it in two steps.



      db.collectionName.find({}, {_id : 1})
      .limit(100)
      .sort({timestamp:-1})
      .toArray()
      .map(function(doc) { return doc._id; }); // Pull out just the _ids


      Then pass the returned _ids to the remove method:



      db.collectionName.remove({_id: {$in: removeIdsArray}})


      FYI: you cannot remove documents from a capped collection.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited May 31 '17 at 14:30









      Nuri Tasdemir

      7,93512545




      7,93512545










      answered Sep 28 '13 at 11:15









      WiredPrairieWiredPrairie

      46.1k1384121




      46.1k1384121













      • Who do you have to do the sort part if all the records are going to be deleted?

        – ULazdins
        Jul 22 '14 at 12:52






      • 2





        Correct me if I'm wrong, but it makes difference whether limit(100) comes before sort({timestamp:-1}) or after. In the example @WiredPrairie limits results to 100 and sorts them afterwards (.sort({timestamp:-1}).limit(100)). In this case the sorting statement has no effect on the records returned. On the other hand, if you would write .limit(100).sort({timestamp:-1}), it would make a difference.

        – ULazdins
        Oct 14 '15 at 14:46








      • 10





        @ULazdins "Mongo applies the sort before limiting the results regardless of the order you call sort and limit on the cursor." (source)

        – joeytwiddle
        Apr 5 '16 at 3:37











      • @Madbreaks Afforementioned "new" answer is wrong. Read the comment under that.

        – Nuri Tasdemir
        May 31 '17 at 14:31











      • what if the numbers is large? I got the error : "Converting from JavaScript to BSON failed: Object size 19888938 exceeds limit of 16793600 bytes. "

        – Jaskey
        Sep 13 '17 at 6:14



















      • Who do you have to do the sort part if all the records are going to be deleted?

        – ULazdins
        Jul 22 '14 at 12:52






      • 2





        Correct me if I'm wrong, but it makes difference whether limit(100) comes before sort({timestamp:-1}) or after. In the example @WiredPrairie limits results to 100 and sorts them afterwards (.sort({timestamp:-1}).limit(100)). In this case the sorting statement has no effect on the records returned. On the other hand, if you would write .limit(100).sort({timestamp:-1}), it would make a difference.

        – ULazdins
        Oct 14 '15 at 14:46








      • 10





        @ULazdins "Mongo applies the sort before limiting the results regardless of the order you call sort and limit on the cursor." (source)

        – joeytwiddle
        Apr 5 '16 at 3:37











      • @Madbreaks Afforementioned "new" answer is wrong. Read the comment under that.

        – Nuri Tasdemir
        May 31 '17 at 14:31











      • what if the numbers is large? I got the error : "Converting from JavaScript to BSON failed: Object size 19888938 exceeds limit of 16793600 bytes. "

        – Jaskey
        Sep 13 '17 at 6:14

















      Who do you have to do the sort part if all the records are going to be deleted?

      – ULazdins
      Jul 22 '14 at 12:52





      Who do you have to do the sort part if all the records are going to be deleted?

      – ULazdins
      Jul 22 '14 at 12:52




      2




      2





      Correct me if I'm wrong, but it makes difference whether limit(100) comes before sort({timestamp:-1}) or after. In the example @WiredPrairie limits results to 100 and sorts them afterwards (.sort({timestamp:-1}).limit(100)). In this case the sorting statement has no effect on the records returned. On the other hand, if you would write .limit(100).sort({timestamp:-1}), it would make a difference.

      – ULazdins
      Oct 14 '15 at 14:46







      Correct me if I'm wrong, but it makes difference whether limit(100) comes before sort({timestamp:-1}) or after. In the example @WiredPrairie limits results to 100 and sorts them afterwards (.sort({timestamp:-1}).limit(100)). In this case the sorting statement has no effect on the records returned. On the other hand, if you would write .limit(100).sort({timestamp:-1}), it would make a difference.

      – ULazdins
      Oct 14 '15 at 14:46






      10




      10





      @ULazdins "Mongo applies the sort before limiting the results regardless of the order you call sort and limit on the cursor." (source)

      – joeytwiddle
      Apr 5 '16 at 3:37





      @ULazdins "Mongo applies the sort before limiting the results regardless of the order you call sort and limit on the cursor." (source)

      – joeytwiddle
      Apr 5 '16 at 3:37













      @Madbreaks Afforementioned "new" answer is wrong. Read the comment under that.

      – Nuri Tasdemir
      May 31 '17 at 14:31





      @Madbreaks Afforementioned "new" answer is wrong. Read the comment under that.

      – Nuri Tasdemir
      May 31 '17 at 14:31













      what if the numbers is large? I got the error : "Converting from JavaScript to BSON failed: Object size 19888938 exceeds limit of 16793600 bytes. "

      – Jaskey
      Sep 13 '17 at 6:14





      what if the numbers is large? I got the error : "Converting from JavaScript to BSON failed: Object size 19888938 exceeds limit of 16793600 bytes. "

      – Jaskey
      Sep 13 '17 at 6:14













      0














      Another way is to write a python script.



      from pymongo import MongoClient

      def main():
      local_client = MongoClient()
      collection = local_client.database.collection
      cursor = collection.find()
      total_number_of_records = 10000

      for document in cursor:
      id = document.get("_id")

      if total_number_of_records == 100:
      break

      delete_query = {"_id": id}
      collection.delete_one(delete_query)

      total_number_of_records -= 1

      if __name__ == "__main__":
      # execute only if run as a script
      main()





      share|improve this answer






























        0














        Another way is to write a python script.



        from pymongo import MongoClient

        def main():
        local_client = MongoClient()
        collection = local_client.database.collection
        cursor = collection.find()
        total_number_of_records = 10000

        for document in cursor:
        id = document.get("_id")

        if total_number_of_records == 100:
        break

        delete_query = {"_id": id}
        collection.delete_one(delete_query)

        total_number_of_records -= 1

        if __name__ == "__main__":
        # execute only if run as a script
        main()





        share|improve this answer




























          0












          0








          0







          Another way is to write a python script.



          from pymongo import MongoClient

          def main():
          local_client = MongoClient()
          collection = local_client.database.collection
          cursor = collection.find()
          total_number_of_records = 10000

          for document in cursor:
          id = document.get("_id")

          if total_number_of_records == 100:
          break

          delete_query = {"_id": id}
          collection.delete_one(delete_query)

          total_number_of_records -= 1

          if __name__ == "__main__":
          # execute only if run as a script
          main()





          share|improve this answer















          Another way is to write a python script.



          from pymongo import MongoClient

          def main():
          local_client = MongoClient()
          collection = local_client.database.collection
          cursor = collection.find()
          total_number_of_records = 10000

          for document in cursor:
          id = document.get("_id")

          if total_number_of_records == 100:
          break

          delete_query = {"_id": id}
          collection.delete_one(delete_query)

          total_number_of_records -= 1

          if __name__ == "__main__":
          # execute only if run as a script
          main()






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 20 '18 at 19:49

























          answered Nov 20 '18 at 18:40









          jellyDeanjellyDean

          5010




          5010























              0














              Let N be number of records to delete.



                  db.collectionName.find().limit(N).forEach(doc => 
              {
              db.collectionName.remove({_id:doc._id})
              }
              )





              share|improve this answer




























                0














                Let N be number of records to delete.



                    db.collectionName.find().limit(N).forEach(doc => 
                {
                db.collectionName.remove({_id:doc._id})
                }
                )





                share|improve this answer


























                  0












                  0








                  0







                  Let N be number of records to delete.



                      db.collectionName.find().limit(N).forEach(doc => 
                  {
                  db.collectionName.remove({_id:doc._id})
                  }
                  )





                  share|improve this answer













                  Let N be number of records to delete.



                      db.collectionName.find().limit(N).forEach(doc => 
                  {
                  db.collectionName.remove({_id:doc._id})
                  }
                  )






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 27 '18 at 12:32









                  Prashant SharmaPrashant Sharma

                  616




                  616























                      -5














                      Below query will find and delete the latest 10 documents from collection:-



                      db.collectionsname.findAndModify({
                      query: { 'status':0 },
                      sort: { 'timestamp': -1 },
                      limit: 10,
                      remove: true
                      });





                      share|improve this answer





















                      • 5





                        docs.mongodb.com/manual/reference/method/… This doesn't support a "limit" field, and in fact if you try it, you will find it only deletes 1 element (as that is the default logic)

                        – tweak2
                        May 15 '17 at 16:33
















                      -5














                      Below query will find and delete the latest 10 documents from collection:-



                      db.collectionsname.findAndModify({
                      query: { 'status':0 },
                      sort: { 'timestamp': -1 },
                      limit: 10,
                      remove: true
                      });





                      share|improve this answer





















                      • 5





                        docs.mongodb.com/manual/reference/method/… This doesn't support a "limit" field, and in fact if you try it, you will find it only deletes 1 element (as that is the default logic)

                        – tweak2
                        May 15 '17 at 16:33














                      -5












                      -5








                      -5







                      Below query will find and delete the latest 10 documents from collection:-



                      db.collectionsname.findAndModify({
                      query: { 'status':0 },
                      sort: { 'timestamp': -1 },
                      limit: 10,
                      remove: true
                      });





                      share|improve this answer















                      Below query will find and delete the latest 10 documents from collection:-



                      db.collectionsname.findAndModify({
                      query: { 'status':0 },
                      sort: { 'timestamp': -1 },
                      limit: 10,
                      remove: true
                      });






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Jul 6 '17 at 13:50









                      Sergio Tulentsev

                      180k30289305




                      180k30289305










                      answered Apr 12 '17 at 5:31









                      anonymusanonymus

                      253




                      253








                      • 5





                        docs.mongodb.com/manual/reference/method/… This doesn't support a "limit" field, and in fact if you try it, you will find it only deletes 1 element (as that is the default logic)

                        – tweak2
                        May 15 '17 at 16:33














                      • 5





                        docs.mongodb.com/manual/reference/method/… This doesn't support a "limit" field, and in fact if you try it, you will find it only deletes 1 element (as that is the default logic)

                        – tweak2
                        May 15 '17 at 16:33








                      5




                      5





                      docs.mongodb.com/manual/reference/method/… This doesn't support a "limit" field, and in fact if you try it, you will find it only deletes 1 element (as that is the default logic)

                      – tweak2
                      May 15 '17 at 16:33





                      docs.mongodb.com/manual/reference/method/… This doesn't support a "limit" field, and in fact if you try it, you will find it only deletes 1 element (as that is the default logic)

                      – tweak2
                      May 15 '17 at 16:33


















                      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%2f19065615%2fhow-to-delete-n-numbers-of-documents-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

                      MongoDB - Not Authorized To Execute Command

                      in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

                      Npm cannot find a required file even through it is in the searched directory