In this one we are going to connect cloud mongodb with python.
### Setup We need to install two packages which are: 1. `pymongo` 2. `dnspython` To install these run the following commands: ````shell pip install dnspython pip install pymongo ````- Go to MongoDB create an account or login if you already have an existing one:
- The instructions on how to create a cluster can be found Here
- after everything is seted up in the cloud now we can work with python code
- We need a connection string that will be found during creation of the cluster.
- We also need a password which will be found during setup as well
import pymongo
password ="YOUR_PASSWORD"
databaseName = "customers"
connection_url = f'mongodb+srv://crispen:{password}@cluster0.zzr2y.mongodb.net/{databaseName}?retryWrites=true&w=majority'
client = pymongo.MongoClient(connection_url)
cursor = client.list_database_names()
print(cursor)
- To insert documents in the collection we use the following methods:
insert_one()
insert_many()
- This method insert one document in the collection name example:
db = client.blob
cursor = db.customers.insert_one({"name":"name1"})
print("Inserted", cursor.inserted_id)
- This method insert multiple documents in the collection:
db = client.blob
customersInfo = [
{"name":"customer3"}, {"name": "customer100"}
]
cursor = db.customers.insert_many(customersInfo)
print("Inserted", cursor.inserted_ids)
- There are two methods in mongoDB that are used to query documents from a collection which are:
find()
findOne()
- used to get a single document that occurs first in a collection
cursor = db.customers.find_one({"name":"customer3"})
print(cursor)
- get all the documents that meet a certain condition:
db = client.blob
cursor = db.customers.find({"name":"customer3"})
for doc in cursor:
print(doc)
Listing all the docs in the collection:
db = client.blob
cursor = db.customers.find({})
for doc in cursor:
print(doc)
- In mongoDb the
sort()
method is used to sort the documents. For example let's sort our documents base on name:
db = client.blob
cursor = db.customers.find().sort("name")
for doc in cursor:
print(doc)
OR
cursor = db.customers.find().sort("name", 1)
for doc in cursor:
print(doc)
db = client.blob
cursor = db.customers.find().sort("name", -1)
for doc in cursor:
print(doc)
- To delete documents we use the following methods:
delete_one()
delete_many()
- To delete one document we use the
delete_one()
method the following is an example on how to delete a document that matches a condition:
db = client.blob
cursor = db.customers.delete_one({"name": "customer100"})
print(cursor.raw_result)
- To delete many document we use the
delete_many()
method the following is an example on how to delete a documents that matches a condition:
db = client.blob
cursor = db.customers.delete_many({"name": "customer3"})
print(cursor.raw_result)
- Dropping a collection means deleting a collection from a database. Let's delete our collection
customers
db = client.blob
cursor = db.customers.drop()
print(db.collection_names()) # []
print("Collection deleted")
- To update a collection we use one of the methods which are:
update_one()
update_many()
- to update a single collection that matches a query we do it as follows:
db = client.blob
cursor = db.customers.update_one({"name": "Name1"}, {"$set": {"name": "Name100"}})
print(cursor.raw_result, cursor.modified_count)
- to update all collections that matches a query we do it as follows:
db = client.blob
cursor = db.customers.update_one({"name": "Name2"}, {"$set": {"name": "Name200"}})
print(cursor.raw_result, cursor.modified_count)
To limit the results we use the limit()
method which takes one parameter an integer which is the total number of documents that will be returned:
Suppose we have the following documents in our collection and we want to get only first 5 customers we can do it as follows:
{'_id': 1, 'name': 'John', 'address': 'Highway37'}
{'_id': 2, 'name': 'Peter', 'address': 'Lowstreet 27'}
{'_id': 3, 'name': 'Amy', 'address': 'Apple st 652'}
{'_id': 4, 'name': 'Hannah', 'address': 'Mountain 21'}
{'_id': 5, 'name': 'Michael', 'address': 'Valley 345'}
{'_id': 6, 'name': 'Sandy', 'address': 'Ocean blvd 2'}
{'_id': 7, 'name': 'Betty', 'address': 'Green Grass 1'}
{'_id': 8, 'name': 'Richard', 'address': 'Sky st 331'}
{'_id': 9, 'name': 'Susan', 'address': 'One way 98'}
{'_id': 10, 'name': 'Vicky', 'address': 'Yellow Garden 2'}
{'_id': 11, 'name': 'Ben', 'address': 'Park Lane 38'}
{'_id': 12, 'name': 'William', 'address': 'Central st 954'}
{'_id': 13, 'name': 'Chuck', 'address': 'Main Road 989'}
{'_id': 14, 'name': 'Viola', 'address': 'Sideway 1633'}
db = client.blob
cursor = db.customers.find().limit(5)
for doc in cursor:
print(doc)
That's the basics about python + cloud mongodb using
pymongo