Title : Python MongoDB
let's go through each topic in detail, including explanations and examples:
MongoDB:
MongoDB stores data in JSON-like documents, making it flexible and scalable.
To start experimenting with MongoDB, you need access to a MongoDB database.
You can download a free MongoDB database from MongoDB.
Alternatively, you can use a MongoDB cloud service like MongoDB Atlas.
PyMongo:
Python needs a MongoDB driver to access MongoDB databases.
In this tutorial, we'll use the MongoDB driver "PyMongo".
We recommend using PIP to install "PyMongo".
If you haven't already installed PyMongo, you can do so by navigating to your command line and typing:
python -m pip install pymongo
Now, let's proceed to cover each topic with examples:
- MongoDB Get Started:
MongoDB Get Started involves connecting to a MongoDB server and starting to work with databases and collections.
import pymongo
# Establish connection to MongoDB server
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
# Check available databases
print(myclient.list_database_names())
This code connects to a local MongoDB server and lists available databases.
- MongoDB Create Database:
MongoDB doesn't require explicit database creation. Databases are created implicitly when data is inserted into them.
- MongoDB Create Collection:
Collections are created implicitly when documents are inserted into them.
- MongoDB Insert:
Inserting data into MongoDB is done using the insert_one()
or insert_many()
methods.
# Access or create database
mydb = myclient["mydatabase"]
# Access or create collection
mycol = mydb["customers"]
# Insert a document
mydict = { "name": "John", "address": "Highway 37" }
x = mycol.insert_one(mydict)
print(x.inserted_id)
This code inserts a document into the "customers" collection and prints the inserted document's ID.
- MongoDB Find:
Retrieving data from MongoDB is done using the find()
method.
# Find all documents in the collection
for x in mycol.find():
print(x)
This code finds and prints all documents in the "customers" collection.
- MongoDB Query:
You can query documents based on specific criteria using the find()
method with a query object.
# Find documents where the name is "John"
myquery = { "name": "John" }
mydoc = mycol.find(myquery)
for x in mydoc:
print(x)
This code queries and prints documents where the name is "John".
- MongoDB Sort:
Sorting results in MongoDB is done using the sort()
method.
# Sort documents by the "name" field in ascending order
mydoc = mycol.find().sort("name")
for x in mydoc:
print(x)
This code sorts and prints documents in the "customers" collection by the "name" field.
- MongoDB Delete:
Deleting documents in MongoDB is done using the delete_one()
or delete_many()
methods.
# Delete the document where the name is "John"
myquery = { "name": "John" }
mycol.delete_one(myquery)
This code deletes the document where the name is "John" from the "customers" collection.
- MongoDB Drop Collection:
You can drop a collection in MongoDB using the drop()
method.
# Drop the "customers" collection
mycol.drop()
This code drops the "customers" collection from the database.
-
MongoDB Update:
Updating documents in MongoDB is done using the
update_one()
orupdate_many()
methods.
# Update the document where the name is "John" myquery = { "name": "John" } newvalues = { "$set": { "address": "Canyon 123" } } mycol.update_one(myquery, newvalues)
This code updates the address of the document where the name is "John" to "Canyon 123".
-
MongoDB Limit:
Limiting results in MongoDB is done using the
limit()
method.
# Limit the number of documents returned to 5 myresult = mycol.find().limit(5) for x in myresult: print(x)
This code limits and prints the first 5 documents in the "customers" collection.
Ensure you have a MongoDB server running locally or have access to a MongoDB cloud service. Also, replace any placeholder values like database names, collection names, etc., with actual values. Additionally, proper error handling should be implemented for robustness.
Top comments (0)