Hackerss.com

hackerss
hackerss

Posted on

Python Connect to mongodb database


#import pymongo
from pymongo import MongoClient

#connect to mongodb
client = MongoClient('localhost', 27017)

#create a database
db = client.test_database

#create a collection
collection = db.test_collection

#insert a document
collection.insert_one({'name': 'Raj'})

#insert multiple documents
collection.insert_many([{'name': 'Raj'}, {'name': 'Raj'}])

#find all documents in the collection
cursor = collection.find()
for document in cursor:
    print(document)



Enter fullscreen mode Exit fullscreen mode

Top comments (0)