0

I was trying to save data from jupyter notebook in Visual Studio Code into my local mongoDB database. Previously, I had multiple databases in my local mongoDB server. However, after running the pymongo code, all the existing databases got deleted and only the new database that was created in the python code exists. Is there a reason because of which this has occurred? How can I resolve it?

Here's the code I had used for the same:
# Making a Connection with MongoClient
client = MongoClient("mongodb://localhost:27017/")
# database
db = client["stocks_database"]
# collection
company= db["Company"]

db.list_collection_names()

company.insert_one({"index":"Sensex","data":'abc'})
rickhg12hs
  • 10,638
  • 6
  • 24
  • 42
  • Are you confusing _databases_ and _collections_? What is `print(list(client.list_databases()))`? – rickhg12hs Jan 14 '23 at 10:50
  • @rickhg12hs I had written that piece of code to test...anyways, it returns a list of all the collections in the database. – PRATIKSHA NARASIMHA NAYAK G Jan 15 '23 at 05:19
  • `print(list(client.list_databases()))` will print all the _**databases**_. _"I dont know why it deleted my existing databases"_ The code you've shown didn't delete any databases. Maybe some other code did, we don't know. – rickhg12hs Jan 15 '23 at 05:23

0 Answers0