Unit 5 Lab Programs Ex - No.5.1 To 5.3
Unit 5 Lab Programs Ex - No.5.1 To 5.3
1. https://www.mongodb.com/try/download/community
7.
8.
9.Open MongoCompass
10.Click Connect
11.Open IDLE
Coding:
import pymongo
client=pymongo.MongoClient('mongodb://localhost:27017')
print(client)
alldb=client.list_database_names()
print(alldb)
O/p:
Ex.No:5.2.Use a cursor to iterate over the records in a collection/table and
print specific fields/attributes
Algorithm:
1.Import pymongo
2.Connect with Mongocompas using MongoClient command
3.Create a ‘Employee’ data base
4.Create a collection ‘Information’
5.Create a document rec with
attributes{EMPNO,ENAME,JOB,HIREDATE,SAL,DEPTNO}
6.Finally insert the document into the collection using Insert_one or
insert_many command.
7.Open the Mongodb .The created collection will be displayed.
Program:
import pymongo
client=pymongo.MongoClient('mongodb://localhost:27017')
mydb=client['Employee']
information=mydb.table
rec={
"EMPNO" : 7934,
"ENAME" : "AAAAA",
"JOB" : "CLERK",
"HIREDATE" : "1.08.2018",
"SAL" : 35000,
"DEPTNO" : 10
doc=information.insert_one(rec)
print(doc)
import pymongo
client=pymongo.MongoClient('mongodb://localhost:27017')
mydb=client['Employee']
information=mydb.table
rec=[{
"EMPNO" : 7934,
"ENAME" : "AAAAA",
"JOB" : "CLERK",
"HIREDATE" : "1.08.2018",
"SAL" : 35000,
"DEPTNO" : 10
},
"EMPNO" : 7935,
"ENAME" : "BBBB",
"JOB" : "CODER",
"HIREDATE" : "1.08.2019",
"SAL" : 55000,
"DEPTNO" : 5
},
"EMPNO" : 7936,
"ENAME" : "CCCC",
"JOB" : "MANAGER",
"HIREDATE" : "1.08.2008",
"SAL" : 100000,
"DEPTNO" : 1 }
doc=information.insert_many(rec)
print(doc)
o/p:
import pymongo
client=pymongo.MongoClient('mongodb://localhost:27017')
mydb=client['Employee']
information=mydb.table
print(read)
o/p:
import pymongo
client=pymongo.MongoClient('mongodb://localhost:27017')
mydb=client['Employee']
information=mydb.table
dis=information.find()
print(record)
O/P:
Exp:5.3 Implement error handling for specific scenarios, such as duplicate key
violation or record not found, in the NoSQL database.
Program:
from pymongo import MongoClient
try:
client=MongoClient('mongodb://localhost:27017')
db = client['Employee']
collection = db['mm']
except CollectionInvalid as e: