print('After Prinitng')
q5 = { 'address': 'Highway 37'}
col.delete_many(q5)
for x in col.find():
print(x)
"""
After Prinitng
{'_id': ObjectId('62bd5f3cf32b68f5a744b5ab'), 'address': 'Highway 38'}\
"""
Before and After dropping collections
import pymongo
# creating a client
c = pymongo.MongoClient("mongodb://localhost:27017/")
db = c['db1']
col = db["customers"]
many = [
{ "_id": 1, "name": "AMET", "address": "ECR, Chennai"},
{ "_id": 2, "name": "ABS", "address": "Anna Nagar, Chennai"},
]
x = col.insert_many(many)
print('Before dropping')
for x in col.find():
print(x)
col.drop()
print('After dropping')
for x in col.find():
print(x)
"""
Before dropping
{'_id': 1, 'name': 'AMET', 'address': 'ECR, Chennai'}
{'_id': 2, 'name': 'ABS', 'address': 'Anna Nagar, Chennai'}
After dropping
"""
Sorting Collections
import pymongo
# creating a client
c = pymongo.MongoClient("mongodb://localhost:27017/")
db = c['db1']
col = db["customers"]
col.drop()
many = [
{ "_id": 11, "name": "AMET", "address": "ECR, Chennai"},
{ "_id": 21, "name": "ABS", "address": "Anna Nagar, Chennai"},
]
x = col.insert_many(many)
print('Before Sorting')
for x in col.find():
print(x)
doc = col.find().sort("name")
print('After Sorting')
for x in doc:
print(x)
"""
Before Sorting
{'_id': 11, 'name': 'AMET', 'address': 'ECR, Chennai'}
{'_id': 21, 'name': 'ABS', 'address': 'Anna Nagar, Chennai'}
After Sorting
{'_id': 21, 'name': 'ABS', 'address': 'Anna Nagar, Chennai'}
{'_id': 11, 'name': 'AMET', 'address': 'ECR, Chennai'}
"""
Before and After Updating
import pymongo
# creating a client
c = pymongo.MongoClient("mongodb://localhost:27017/")
db = c['db1']
col = db["customers"]
col.drop()
many = [
{ "_id": 11, "name": "AMET", "address": "ECR, Chennai"},
{ "_id": 21, "name": "ABS", "address": "Anna Nagar, Chennai"},
]
x = col.insert_many(many)
q6 = { "address": 'ECR, Chennai' }
newvalues = { "$set": { "address": "#123, East Coast Road, Chennai" } }
print('Before Updation')
for x in col.find():
print(x)
col.update_one(q6, newvalues)
print("After Updation")
#print "customers" after the update:
for x in col.find():
print(x)
"""
Before Updation
{'_id': 11, 'name': 'AMET', 'address': 'ECR, Chennai'}
{'_id': 21, 'name': 'ABS', 'address': 'Anna Nagar, Chennai'}
After Updation
{'_id': 11, 'name': 'AMET', 'address': '#123, East Coast Road, Chennai'}
{'_id': 21, 'name': 'ABS', 'address': 'Anna Nagar, Chennai'}
"""
B4 and After Many Updating using regular expression
import pymongo
client = pymongo.MongoClient("mongodb://localhost:27017/")
db = client["mydatabase"]
col = db["customers"]
col.drop()
many = [
{ "_id": 10, "name": "AMET", "address": "ECR, Chennai"},
{ "_id": 12, "name": "ABS", "address": "Anna Nagar, Chennai"},
{ "_id": 13, "name": "ABS!@", "address": "ECR, Chennai"},
{ "_id": 14, "name": "Stanford", "address": "Anna Nagar, Chennai"},
]
x = col.insert_many(many)
print('Before Updation')
for x in col.find():
print(x)
query = { "address": { "$regex": "^A" } }
newvalues = { "$set": { "name": "AMET Updated" } }
x = col.update_many(query, newvalues)
print(x.modified_count, "documents updated.")
print('After many Updation')
for x in col.find():
print(x)
"""
Before Updation
{'_id': 10, 'name': 'AMET', 'address': 'ECR, Chennai'}
{'_id': 12, 'name': 'ABS', 'address': 'Anna Nagar, Chennai'}
{'_id': 13, 'name': 'ABS!@', 'address': 'ECR, Chennai'}
{'_id': 14, 'name': 'Stanford', 'address': 'Anna Nagar, Chennai'}
2 documents updated.
After many Updation
{'_id': 10, 'name': 'AMET', 'address': 'ECR, Chennai'}
{'_id': 12, 'name': 'AMET Updated', 'address': 'Anna Nagar, Chennai'}
{'_id': 13, 'name': 'ABS!@', 'address': 'ECR, Chennai'}
{'_id': 14, 'name': 'AMET Updated', 'address': 'Anna Nagar, Chennai'}
"""
After using limit to display
# limit parameter to limit two records
output = col.find().limit(2)
print('After setting Limit(2)')
for x in output:
print(x)
"""
After setting Limit(2)
{'_id': 10, 'name': 'AMET', 'address': 'ECR, Chennai'}
{'_id': 12, 'name': 'AMET Updated', 'address': 'Anna Nagar, Chennai'}
"""
Bon Appetite and Happy MongoDB Coding....Learning!!!