MongaDB Features
- Full cloud-based application data platform.
- Flexible document schemas.
- JSON..BSON (BJSON)..
- Widely supported and code-native data access.
- Change-friendly design.
- Powerful querying and analytics.
- Easy horizontal scale-out with sharding.
- Simple installation.
- Cost-effective.
Learning to use MongoDB
Step 1:
import pymonga
step 3: list database names
# import library
import pymongo
# creating a client
myc = pymongo.MongoClient("mongodb://localhost:27017/")
# Just printing existing databases
print(myc.list_database_names()) # ['admin', 'config', 'db1', 'local']
Step 4: Create Collections
# import library
import pymongo
# creating a client
c = pymongo.MongoClient("mongodb://localhost:27017/")
# Just printing existing databases
print(c.list_database_names()) # ['admin', 'config', 'db1', 'local']
# choose any demo db as db1
# create collection customers in db1
db = c['db1']
col = db["customers"]
print(db.list_collection_names())
# add Dict data
dict = { "name": "John", "address": "Highway 37" }
x = col.insert_one(dict)
print(x.inserted_id)
#add Dict1 data
dict2 = { "name": "Peter", "address": "Lowstreet 27" }
y = col.insert_one(dict2)
print(y.inserted_id)
"""
['admin', 'config', 'db1', 'local']
['customers']
62bd57445c7e7af908e87efa
62bd57445c7e7af908e87efb
"""
To display with Object_ID
clist = db.list_collection_names()
if "customers" in clist:
print("The collection exists.")
for x in col.find():
print(x)
"""
['admin', 'config', 'local']
[]
62bd58d899982b27053eb9cc
62bd58d899982b27053eb9cd
The collection exists.
{'_id': ObjectId('62bd58d899982b27053eb9cc'), 'name': 'John', 'address': 'Highway 37'}
{'_id': ObjectId('62bd58d899982b27053eb9cd'), 'name': 'Peter', 'address': 'Lowstreet 27'}
"""
Before and After deleting
# import library
import pymongo
# creating a client
c = pymongo.MongoClient("mongodb://localhost:27017/")
db = c['db1']
col = db["customers"]
col.drop()
q1 = { 'address': 'Highway 37'}
q2 = { 'address': 'Highway 38'}
q3 = { 'address': 'Highway 39'}
print(10*'-', 'Insering 3 qs')
col.insert_one(q1)
col.insert_one(q2)
col.insert_one(q3)
print(10*'-', 'Before Delete')
for x in col.find():
print(x)
q4 = { 'address': 'Highway 39'}
col.delete_many(q4)
print(10*'-', 'After Delete')
for x in col.find():
print(x)
"""
---------- Insering 3 qs
---------- Before Delete
{'_id': ObjectId('62bd5db330e379663c8851ce'), 'address': 'Highway 37'}
{'_id': ObjectId('62bd5db430e379663c8851cf'), 'address': 'Highway 38'}
{'_id': ObjectId('62bd5db430e379663c8851d0'), 'address': 'Highway 39'}
---------- After Delete
{'_id': ObjectId('62bd5db330e379663c8851ce'), 'address': 'Highway 37'}
{'_id': ObjectId('62bd5db430e379663c8851cf'), 'address': 'Highway 38'}
"""
After deleting another Record
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!!!
No comments:
Post a Comment