PYTHON Tutorial
pip install pymongo
from pymongo import MongoClient
client = MongoClient("mongodb://username:password@host:port")
db = client["mydb"]
collection = db["mycollection"]
pip install mongoengine
from mongoengine import connect
connect("mongodb://username:password@host:port", alias="default")
class Person(Document):
name = StringField()
age = IntField()
This will create a Person
collection in MongoDB.Method | Library |
---|---|
Find documents | find() , find_one() |
Insert documents | insert_one() , insert_many() |
Update documents | update_one() , update_many() |
Delete documents | delete_one() , delete_many() |
from pymongo import MongoClient
client = MongoClient("mongodb://localhost:27017")
db = client["mydb"]
collection = db["mycollection"]
# Insert a document
collection.insert_one({"name": "John Doe", "age": 30})
# Find a document
document = collection.find_one({"name": "John Doe"})
# Print the document
print(document)