- this is a quick walk through python working with mongoD
- First you need to download and install MongoDB database
- Install pymongo
- to install pymongo run the command
pip install pymongo
To connect to mongodb it is simple as doing the following:
import pymongo
client = pymongo.MongoClient(host="localhost", port=27017)
OR
import pymongo
client = pymongo.MongoClient("mongodb://localhost:27017/")
In mongoDB a database is only created if and only if it has contents in it:
- Creating a new database in mongoDb is simple as doing the following
import pymongo
client = pymongo.MongoClient(host="localhost", port=27017)
db = client["shop"]
- To list all created database using pymongo we do it as follows
print(client.list_database_names())
Just like creating a database a collection will be created if and only if it contains some contents in it.
- Let's create a collection
customers
in our database.
- To lists all the collections that are in the database we do it as follows:
print(db.collection_names())
- 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:
import pymongo
client = pymongo.MongoClient(host="localhost", port=27017)
db = client["shop"]
customers = db["customers"]
customer ={
"name":"customer1",
}
cursor = customers.insert_one(customer)
print("Inserted", cursor.inserted_id)
- This method insert multiple documents in the collection:
import pymongo
client = pymongo.MongoClient(host="localhost", port=27017)
db = client["shop"]
customers = db["customers"]
customersInfo =[
{"name":"customer3"}, {"name":"customer100"}
]
cursor = customers.insert_many(customersInfo)
print(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 = customers.find_one({"name":"customer3"})
print(cursor)
- get all the documents that meet a certain condition:
cursor = customers.find({"name":"customer3"})
for doc in cursor:
print(doc)
Listing all the docs in the collection:
cursor = 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:
cursor = customers.find().sort("name")
for doc in cursor:
print(doc)
OR
cursor = customers.find().sort("name", 1)
for doc in cursor:
print(doc)
cursor = 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:
cursor = 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:
cursor = 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
cursor = 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:
cursor = 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:
cursor = 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'}
cursor = customers.find().limit(5)
for doc in cursor:
print(doc)
That's the basics about python + mongodb using
pymongo