0% found this document useful (0 votes)
15 views

Unit 5 Lab Programs Ex - No.5.1 To 5.3

Uploaded by

srinivasan.cse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Unit 5 Lab Programs Ex - No.5.1 To 5.3

Uploaded by

srinivasan.cse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Ex.No : 5.

1 Connect to the NoSQL database using a Python connector module,


such as "pymongo" for MongoDB or "cassandra-driver" for Cassandra.

1. https://www.mongodb.com/try/download/community

Click The Select Package


2.

Click download, then the mongodb has been downloaded.

3.Open the exe file from download ,then click next.


4.In next window select the complete,then click next.

5.Mongodb will be downloaded in the PC.

6.Open command prompt

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)

12.Run the program

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)

//Create a More one collection

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:

//find the collection

import pymongo

client=pymongo.MongoClient('mongodb://localhost:27017')

mydb=client['Employee']
information=mydb.table

read = information.find_one({"ENAME": "CCCC"})

print(read)

o/p:

Find all the collection

import pymongo

client=pymongo.MongoClient('mongodb://localhost:27017')

mydb=client['Employee']

information=mydb.table

#print the collection in the database

dis=information.find()

for record in dis:

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

from pymongo.errors import CollectionInvalid

try:

client=MongoClient('mongodb://localhost:27017')

db = client['Employee']

collection = db['mm']

except CollectionInvalid as e:

print(f"Collection not found error: {e}")

You might also like