how to drop data in mongo db using python












1















In my mongodb I have around 20K records,
now From the same mongodb I am importing only 500 records in python and now I need to delete those 500 records which I have imported , is it possible to delete those 500 records using python?



my code for importing 500 records



from pymongo import MongoClient
con = MongoClient(local host, 27017)
db = con.rssfeeds_db
data = pd.DataFrame(list(db.restdata.find().limit(500)))









share|improve this question



























    1















    In my mongodb I have around 20K records,
    now From the same mongodb I am importing only 500 records in python and now I need to delete those 500 records which I have imported , is it possible to delete those 500 records using python?



    my code for importing 500 records



    from pymongo import MongoClient
    con = MongoClient(local host, 27017)
    db = con.rssfeeds_db
    data = pd.DataFrame(list(db.restdata.find().limit(500)))









    share|improve this question

























      1












      1








      1








      In my mongodb I have around 20K records,
      now From the same mongodb I am importing only 500 records in python and now I need to delete those 500 records which I have imported , is it possible to delete those 500 records using python?



      my code for importing 500 records



      from pymongo import MongoClient
      con = MongoClient(local host, 27017)
      db = con.rssfeeds_db
      data = pd.DataFrame(list(db.restdata.find().limit(500)))









      share|improve this question














      In my mongodb I have around 20K records,
      now From the same mongodb I am importing only 500 records in python and now I need to delete those 500 records which I have imported , is it possible to delete those 500 records using python?



      my code for importing 500 records



      from pymongo import MongoClient
      con = MongoClient(local host, 27017)
      db = con.rssfeeds_db
      data = pd.DataFrame(list(db.restdata.find().limit(500)))






      python mongodb import






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 '18 at 15:24









      Rahul VarmaRahul Varma

      1378




      1378
























          1 Answer
          1






          active

          oldest

          votes


















          1














          You would need to use the _id for each record so that you could tell pymongo which records to delete. For example:



          from pymongo import MongoClient
          con = MongoClient(localhost, 27017)
          db = con.rssfeeds_db

          // create a list of the records. each one will be a dict.
          records = [x for x in db.restdata.find().limit(500)]

          // create a list of the 500 ids so we only need to call the db once
          record_ids = [record['_id'] for record in records]

          //create your dataframe
          data = pd.DataFrame(records)

          // delete all 500 records at once
          db.restdata.delete_many(
          {'_id':
          {'$in': record_ids}
          },
          )





          share|improve this answer


























          • hi @forgetso I am getting error for the above code TypeError: 'Collection' object is not callable. If you meant to call the 'deleteMany' method on a 'Collection' object it is failing because no such method exists.

            – Rahul Varma
            Nov 14 '18 at 15:44













          • My bad, it should be delete_many for pymongo (deleteMany in Mongo shell). I've updated the answer. Let me know if this works for you.

            – forgetso
            Nov 14 '18 at 15:45













          • Its working fine thank you @forgetso

            – Rahul Varma
            Nov 14 '18 at 15:47













          • Glad to hear it :)

            – forgetso
            Nov 14 '18 at 15:47











          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%2f53303522%2fhow-to-drop-data-in-mongo-db-using-python%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









          1














          You would need to use the _id for each record so that you could tell pymongo which records to delete. For example:



          from pymongo import MongoClient
          con = MongoClient(localhost, 27017)
          db = con.rssfeeds_db

          // create a list of the records. each one will be a dict.
          records = [x for x in db.restdata.find().limit(500)]

          // create a list of the 500 ids so we only need to call the db once
          record_ids = [record['_id'] for record in records]

          //create your dataframe
          data = pd.DataFrame(records)

          // delete all 500 records at once
          db.restdata.delete_many(
          {'_id':
          {'$in': record_ids}
          },
          )





          share|improve this answer


























          • hi @forgetso I am getting error for the above code TypeError: 'Collection' object is not callable. If you meant to call the 'deleteMany' method on a 'Collection' object it is failing because no such method exists.

            – Rahul Varma
            Nov 14 '18 at 15:44













          • My bad, it should be delete_many for pymongo (deleteMany in Mongo shell). I've updated the answer. Let me know if this works for you.

            – forgetso
            Nov 14 '18 at 15:45













          • Its working fine thank you @forgetso

            – Rahul Varma
            Nov 14 '18 at 15:47













          • Glad to hear it :)

            – forgetso
            Nov 14 '18 at 15:47
















          1














          You would need to use the _id for each record so that you could tell pymongo which records to delete. For example:



          from pymongo import MongoClient
          con = MongoClient(localhost, 27017)
          db = con.rssfeeds_db

          // create a list of the records. each one will be a dict.
          records = [x for x in db.restdata.find().limit(500)]

          // create a list of the 500 ids so we only need to call the db once
          record_ids = [record['_id'] for record in records]

          //create your dataframe
          data = pd.DataFrame(records)

          // delete all 500 records at once
          db.restdata.delete_many(
          {'_id':
          {'$in': record_ids}
          },
          )





          share|improve this answer


























          • hi @forgetso I am getting error for the above code TypeError: 'Collection' object is not callable. If you meant to call the 'deleteMany' method on a 'Collection' object it is failing because no such method exists.

            – Rahul Varma
            Nov 14 '18 at 15:44













          • My bad, it should be delete_many for pymongo (deleteMany in Mongo shell). I've updated the answer. Let me know if this works for you.

            – forgetso
            Nov 14 '18 at 15:45













          • Its working fine thank you @forgetso

            – Rahul Varma
            Nov 14 '18 at 15:47













          • Glad to hear it :)

            – forgetso
            Nov 14 '18 at 15:47














          1












          1








          1







          You would need to use the _id for each record so that you could tell pymongo which records to delete. For example:



          from pymongo import MongoClient
          con = MongoClient(localhost, 27017)
          db = con.rssfeeds_db

          // create a list of the records. each one will be a dict.
          records = [x for x in db.restdata.find().limit(500)]

          // create a list of the 500 ids so we only need to call the db once
          record_ids = [record['_id'] for record in records]

          //create your dataframe
          data = pd.DataFrame(records)

          // delete all 500 records at once
          db.restdata.delete_many(
          {'_id':
          {'$in': record_ids}
          },
          )





          share|improve this answer















          You would need to use the _id for each record so that you could tell pymongo which records to delete. For example:



          from pymongo import MongoClient
          con = MongoClient(localhost, 27017)
          db = con.rssfeeds_db

          // create a list of the records. each one will be a dict.
          records = [x for x in db.restdata.find().limit(500)]

          // create a list of the 500 ids so we only need to call the db once
          record_ids = [record['_id'] for record in records]

          //create your dataframe
          data = pd.DataFrame(records)

          // delete all 500 records at once
          db.restdata.delete_many(
          {'_id':
          {'$in': record_ids}
          },
          )






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 14 '18 at 15:46

























          answered Nov 14 '18 at 15:31









          forgetsoforgetso

          370116




          370116













          • hi @forgetso I am getting error for the above code TypeError: 'Collection' object is not callable. If you meant to call the 'deleteMany' method on a 'Collection' object it is failing because no such method exists.

            – Rahul Varma
            Nov 14 '18 at 15:44













          • My bad, it should be delete_many for pymongo (deleteMany in Mongo shell). I've updated the answer. Let me know if this works for you.

            – forgetso
            Nov 14 '18 at 15:45













          • Its working fine thank you @forgetso

            – Rahul Varma
            Nov 14 '18 at 15:47













          • Glad to hear it :)

            – forgetso
            Nov 14 '18 at 15:47



















          • hi @forgetso I am getting error for the above code TypeError: 'Collection' object is not callable. If you meant to call the 'deleteMany' method on a 'Collection' object it is failing because no such method exists.

            – Rahul Varma
            Nov 14 '18 at 15:44













          • My bad, it should be delete_many for pymongo (deleteMany in Mongo shell). I've updated the answer. Let me know if this works for you.

            – forgetso
            Nov 14 '18 at 15:45













          • Its working fine thank you @forgetso

            – Rahul Varma
            Nov 14 '18 at 15:47













          • Glad to hear it :)

            – forgetso
            Nov 14 '18 at 15:47

















          hi @forgetso I am getting error for the above code TypeError: 'Collection' object is not callable. If you meant to call the 'deleteMany' method on a 'Collection' object it is failing because no such method exists.

          – Rahul Varma
          Nov 14 '18 at 15:44







          hi @forgetso I am getting error for the above code TypeError: 'Collection' object is not callable. If you meant to call the 'deleteMany' method on a 'Collection' object it is failing because no such method exists.

          – Rahul Varma
          Nov 14 '18 at 15:44















          My bad, it should be delete_many for pymongo (deleteMany in Mongo shell). I've updated the answer. Let me know if this works for you.

          – forgetso
          Nov 14 '18 at 15:45







          My bad, it should be delete_many for pymongo (deleteMany in Mongo shell). I've updated the answer. Let me know if this works for you.

          – forgetso
          Nov 14 '18 at 15:45















          Its working fine thank you @forgetso

          – Rahul Varma
          Nov 14 '18 at 15:47







          Its working fine thank you @forgetso

          – Rahul Varma
          Nov 14 '18 at 15:47















          Glad to hear it :)

          – forgetso
          Nov 14 '18 at 15:47





          Glad to hear it :)

          – forgetso
          Nov 14 '18 at 15:47




















          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%2f53303522%2fhow-to-drop-data-in-mongo-db-using-python%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

          Xamarin.iOS Cant Deploy on Iphone

          Glorious Revolution

          Dulmage-Mendelsohn matrix decomposition in Python