Chapter 4-Python Database Interaction Using MongoDB
Chapter 4-Python Database Interaction Using MongoDB
Customers
|M |M M M M M |M M M
M M
dantagesof NoSQL:
NoSQL is well-suited for today's everyincreasing database need because it
alability:
scaled up easily and
with minimum effort. NoSQL database have scalable
be
can
architectureSO it Can efficiently manage data and can scale up to many machines
machines that are required while scaling using of SQL DBMS.
instead of costly
dynamic schema, if we want to change the length of column, or add new column
With need to change the whole table data instead the new data will be stored with
wedon't
structure without affecting the previous data/structure. In NoSQL database,
new
the
insert without predefined schema.
data
wecan
data availability. With multiple
Replication provides redundancy and increases
different database servers, replication protects the database from
copies of data on
losss of single
server. To use replication with sharding, deploy each shared as a replica
set.
across multiple machines and is
, sharding is the process of storing data records
MongoDB's approach to meeting the demand of data growth.
frequently used
. Many NoSQL database have integrated caching mechanism, hence
the need for a
data are stored in system memory as much as possible and discarding
separate caching layer.
4.2 TYPES OF NoSQL
NosQL Databasesare mainly categorized into four types:
1. Key-value Pair Based 2. Column-based
3. Graph-Based 4. Document-based
Every category has its unique attributes and limitations. None of the above-specified
database is better to solve all the problems. Users should select the database based on
heir product needs.
KeyValue Document-Based Column-Based Graph-Based
Graph
Reçords Records
Nodes-Organize Relationships
Have
ExRiaak,mplTokyo
e: Cabinet, Example: MongoDB, Example:
Have
Properties
Peis server, Couch DB, OrientDB, BigTable, Cassandra, Example:
MeMcached, Scalaris RavenDB Hbase,
Hypertable
Neo4J, InfoGrid, Infinite
Graph, Flock DB
4.3
Python Database
Python Programming [MCA - Sem. I] Interaction
using Mongob
4.2.1 Document Based: MongoDB
Document-oriented/Document-based NoSQL DB stores and retrieves
value pair but the value part is stored as a document. The document is data as a key.
or XML formats. The value is understood by the DB and can be queried
Document 1
stored in .SON
Col1 Col2 Col3 Col4 "prop1": data, Document 2
Data Data Data Data
"prop2": data,
"prop3": data, "prop1": data,
Data
Data
Data
Data
Data
Data
Data
Data
"prop4": data "prop2": data,
"prop3": data,
Document3
"prop4": data "orop1" data.
"prop2": data.
"prop3": data.
"prop4": data
Fig.4.4: Relational Database vs Document-oriented Database
In this figure, in left side, you can see we have rows and columns, and in the right es
have a document database which has a similar structure to JSON, Now for the
relational database, you have to krnow what columns you have and so on. However
for a document database, you have data store like JSON object. You do not require to
define which makes it flexible.
The document type is mostly used for CMS systems, Blogging platforms, Real-time
analytics and E-commerce applications. It should not use for complex transactions
which require multiple operations or queries against varying aggregate structures.
Examples: Amazon SimpleDB, CouchDB, MongoDB, Riak, Lotus Notes, MongoDB.
4.2.2 Key-Value Database - Couchbase
Data is stored in key-value pairs. It is designed in such a way to handle lots of data and
heavy load.
Key-value pair storage databases store data as a hash table where each key is uniqu.
and the value can be a jSON,BLOB (Binary Large Objects),
string, etc.
Table 4.1: Examples of Key-value Pairs
Key Value
Name Rahul Patil
Age 20
Occupation Manager
Height 180 cmn databasei
It is one of the most basic NoSQL
used as a collection, dictionaries,
database examples. This kind of NoSQL
Storeshelp
the
associative arrays, etc. Key-value
developer to store schema-less data. They work best for shopping Cart contents.
paper:.
Examples: Redis, Dynamo, Riak. These are all based on Amazon's Dynamo
4.4
IMCA -
Programming Sem. I] Python Database Interaction using MongoD8
an
Couchbase
Couchbaseis a NoSQL distributed document database (SON) With many of the best
featuresof a relational DBMS such as SQL, distributed ACID transactions, and much
more.
has afle
flexible data model, is easily scalable, provides consistent high performance
capable of serving application data with 100% uptime.
andis
Couchbaseisthe merge of two popular NOSQL technologies:
Membase: Membase provides persistence, replication, sharding to the high
performance memcached technology.
CouchDB: CouchDB pioneersthe document oriented model based on JSON.
features:
Flexible D.ata Model: With Couchbase Server, JSON documents are used to represent
1 objects. This document model is
application objects and the relationships between objects without migrating the
fesible enough so that you can change application
application downtime.
database schema, or plan for significant that it is well
The other advantage of the flexible, document-based data model is
representing real-world items. JSON documents support nested structures, as
Suited to
representing relationships between items which enable you to accurately
wellas fields
represent objects in your application. cluster of
Scalability: It is easy to scale with Couchbase Server, both within a additional
2. Easy different data centers. You can add
servers and between clusters at
Couchbase Server to address additional users and growth in application
instances of application code. With one click of a
interruptions or changes in
data without any Couchbase Servers to handle
additional
button, you can quickly grow cluster of
data evenly distributed. Couchbase Server provides automatic
Workload and keep
run time. This allows to resize the server cluster
larding of data and rebalancing at
on demand.
3 Integration: Couchbase provides client libraries for different
aSy Developer
.NET, PHP, Ruby,C, Python, Node.js.
programming languages such as Java,
key-based lookup mechanism where the client is
Prn, Couchbase provides a the server hosting the data (with that key) will
beexpected
to provide the key, and only
contacted.
where the client
mechanism to retrieve data
Couchbase
provides
also provides a query
a query (for example, a range based on some secondary key) as well as the
servers in the cluster and
view broadcast to all
the
For
(basically the index). The query will be
result will be merged and sent back to the client. mechanism where the client sends
request, the
in write,
updated
Couchbase provides a key--based update
When handling
write
with the key (as doc id). as the datais stored in RAM on
the
Server documment
will Tespond to client's write request as soon
active server, which offers the lowest latency for write requests.
4.5
Python Programming [MCA -Sem. I] Python Database Interaction
using
4. Consistent High Performance: Couchbase Server is designed for Mongoba
for setxetrntemelsuby
concurrent data use and consistently high throughput. It provides
millisecond response times which help ensure an enjoyable experience consi
users. By providing consistent, high data throughput, Couchbase Server
Support more users with fewer servers. Couchbase also applicayoutionto
enables
automatically
workload across all servers to maintain consistent performance spreads the
bottlenecks at any given server in a cluster. and reduce
5. Reliable and Secure: Couchbase support access control using
passwords. The credentials are transmitted securely over the network.username and
data can be protected while it is transmitted to/from the client application.The sensitive
There is no single point of failure, since the data can be replicated
nodes. Features such as cross-data center replication, failover, and across multiple
help ensure availability of data during server or datacenter failure. backup and restore
Fundamental Concept:
Couchbase as Document Store:The primary unit of data storage in Couchbase Server
is a JSON document, which makes your application free of
rigidly-defined relational
database tables. Because application objects are modeled as
documents. schema
migrations do not need to be performed.
The binary data can be stored in documents as well, but
using JSON structure allows
the data to be indexed and queried using
views. Couchbase Server provides a
JavaScript-based query engine to find records based on field values.
4.2.3 Wide-column Databases: Cassandra
Column-oriented/Wide-column databases work on columns and are based on BigTable
paper by Google. Every column is treated separately. Values of
databases are stored contiguously. single column
They deliver high performance on aggregation
etc. as the data is readily available in a
queries like SUM, COUNT, AVG, MIN
column.
Column-based NoSQL databases are widely used to manage data
intelligence, CRM, Library card catalogs. warehouses, Dusui
Examples: HBase, Cassandra, HBase, Hypertable are NoSQL
4.2.3.1 Cassandra query.
1. It is a type of NoSQL database. Apache Cassandra is a highly scalable. high-
performance distributed database designed to handle large amounts of data across
many commodity servers, providing high availability with no single point offailure.
Features:
hardwareto
Flexible Scalability: Cassandra is highly scalable. It more
put up more customerS and m0re data as per allows to add
requirement.
4.6
[MCA-
Programmino
Sem. I] Python Database Interaction using MongoDB
Athon Architecture: Cassandra has no single point of failure and it is
Alwayson
continuously.available for business-critical applications that cannot afford afailure.
Linnear-Scale Performance: Cassandra is linearly scalable, i.e.. it increases
Fast
throughput. you
increase the number of nodes in the cluster. Therefore it maintains
as
responsetime.
aquick Distribution: Cassandra provides the flexibility to distribute data where
Data
Easy multiple data centers.
nped by replicating data across
Flexible Data Storage:
Cassandra accommodates all possible data formats including:
structured, semi-structured, and
unstructured. It can dynamically accommodate
data structures according to your need.
changesto
Consistency, Isolation, and
Transaction Support: Cassandra supports Atomicity,
Durability (ACID)properties.
cheap commodity hardware. It
Pact Writes: Cassandra was designed to run on
can store hundreds of terabytes of data, without
performs blazingly fast writes and
sacrificing the read efficiency.
42.4 Graph/Node Databases: Neo4j
relations amongst those entities. The
Agraph type database stores entities as well the
gives a relationship
entity is stored as a node with the relationship as edges. An edge
between nodes. Every node and edge has a unique identifier.
Name: Raj
Knows Knows
Knows
Knows
Knows
Name: Rohan Name: Rahul
Knows
Fig. 4.5: Graph-based Database
Compar ed to a relational database where tables are loosely connected, a Graph-based
database is a multi-relational in nature. Traversing relationship is fast as they are
already captured into the DB, and there is no need to calculate them.
Graph-based database mostly used for social networks, logistics, spatial data.
Exampl es
2.4Ne1o4j NE04}: Neo41, Infinite Graph, OrientDB, FlockDB
pen
is the leading graph database. It's offered commercially, fully supported and
sthemasouroptional
ced. Neo4j is a No-SQL database that is created in 2007. It is Java based,
and massively scalable.
4.7
Python Programming [MCA - Sem. I) Python Databasee
Interaction ulng
Advantages:
1. Flexible Data Model: Neo4j provides a flexible, simple and yet
Mongpa
model. This model can be easily changed according to the powerful
industries.
2. Real-time Insights: Neo4j provides results based on real-time data
applications
3. High Availability: Neo4j is highly available for large
applications with transactional guarantees. enterprise real-time
4. Connected and Semi-structures Data: You can easily represent
semi-structured data using Neo4j.
5. Easy Retrieval: Using Neo4j, you can not only represent but
COnnected and
also easily
(traverse/navigate) connected data faster when comparedto other retrieve
6. Cypher Query Language: Neo4j provides a declarative querydatabases.
represent the graph visually, using8 ascil-art syntax. The commands nÊ 4to language
language are in human readable format and very easy to learn.
7. Not required Joins: Using Neo4j, it does not require complex joins to retrieg
connected/ related data as it is very easy to retrieve its adjacent node or
relationship details without joins or indexes.
4.3 SQL VS NoSQL
Table 4.2: Difference between SQL and NoSQL Databases
Parameters SQL NoSQL
Type Relational Non-relational
Data Structured data stored in tables Un-structured stored in js0N
files but the graph database does
Supports relationship.
Schema Static
Dynamic
Scalability Vertical Horizontal
Language Structured Query Language Unstructured Query Languag
Joins Helpful to design complex| powertul
queries.
No joins, don't have the complex
interface to prepare
query.
OLTP consideredfor
Recommended and
for OLTP system.
best suited Less likely to be
OLTP system. ar
they
Support Great support dependent,model
Community Support
expanding
Supports the: caching
integrated
Integrated Supports in-line memory
caching
4.8
[MCA - Sem. I Python Database Interaction using MongoDB
Programming
SQL NoSQL
farameters
SQLite DynamoDB
Esamples Cassandra
MySQL
Redis
Oracle
CouchDB
PostgreSQL
Microsoft SQL Server RethinkDB
RavenDB
MongoDB
weaknesses
choosing a database, you should consider its strengths and
,When scenario
You also need to consider how the database fits into your specific
carefully.
requirements. Sometimes the right solution is to use a combination
and application's different aspects of a broader system.
of SÌL and NoSQL databases to handle
example,
years, SQL and NoSQL databases have even begun to merge. For
In recent support
systems, such as PostgreSQL, MySQL, and Microsoft SQL Server now
database now
querying JSON data, much like NoSQL databases. With this, you can
storing and don't get many
many of the same results with both technologies. But you still
dchieve
features, such as horizontal scaling and the user-friendly interface.
Or theNosQL
4INTRODUCTION TO MONGODB WITH PYTHON
document-oriented database classified as NoSQL. It is become popular
MongoDB is a and integrates extremely well with Python.
he industry in recent years
Uhlib collections of documents instead of
lahlaitional SØL RDBMSS. MongoDB uses
tables of rows to organize and store data. Here,
flexible JSON-like documents.
MongoDB schemaless and
stores data in can have documents with a different set of fields in the
schemaless meanswithout
same collection, that you
the need for satisfying a rigid table schema.
a
You can change the documents and data over time. This results in
structure of requirement changes without the
lheedexiblefor system that allows you
a complex Process of data quickly adapt to
to migration. However, the trade-off in changing the
the
become inconsistent with
stupdatructeudre schema.new So,documents
of is that exiting
this is atopic
documents
that needs to be managed with care.
MoTnajngoorDB 1S written
platforms,Such as MAC OS, Windows, Solaris, and
most Linux
Inc.
in C++ and actively developed by MongoDB distributions.
It runs on all
4.9
Python Programming (MCA - Sem. I Python Database Interaction
In general, there are three main development goals behind the
using Mongota,
MongoDB
1. Scale well.
2. Store rich data structures.
database.
3. Provide a sophisticated query mechanism.
MongoDB is a distributed database, so high availability,
horizontal
geographic distribution are built into the system. It stores data in
flexibscale ing, and
l
documents. You can model these documents to map the objects in your JSON-ike
which makes it possible to work with your data effectively.
MongoDB provides a powerful query language that supports ad hoc
applications.
aggregation, geospatial search, text search, and a lot more. This queries,
,
providoe % inndexing,
powerful toolkit to access and work with your data. Finally, MongoDB is freo.
available and has great Python support.
Features of MongoDB:
MongoDB offers the following features:
Query support: You can use many standard query types, such as matching (=)
comparison (<,>), and regular expressions.
Data accommodation: You can store virtually any kind of data, be it structured.
partially structured, or even polymorphic.
Scalability: It handles more queries just by adding more machines to the server
cluster.
Flexibility and Agility: You can develop applications with it quickly.
Document Orientation and Schemalessness: You can store all the informauon
regarding a data model in a single document.
Adjustable Schema: You can change the schema of the database on the fly, wn
reduces the time needed to provide new features or fix existing problems.
Relational Database Functionalities: You can perform actions commot
relational databases, like indexing.
MongolDB provides a few tools and features that vou will not find in other datav
systems:
Scalability: Whether you need a stand-alone server or complete clusters o
be.
independent servers, you can scale MongoDB to whatever size youneedittoVarious
Load-balancing support: MongoDB will automatically move data across
shards.
anew
Automatic Failover Support: If your primary server goes down, then
primary will be up and running automatically. Mongol8
Management Tools: You can track your machines using the cloud-based
Management Service (MMS). often
more
a9
INSTALLING MONGODB ON WINDOWS
client["mydb"
MoMotecnongrdgo)DoBDB,before J
a database is not created until it gets
waits until yo
it
content.
nave created a
with at least one
collection
creates
4.11
Python Programming (MCA -Sem. ) Python Database
Interaction using
Check if Database exists:
In MongoDB, a database is not created until it gets content. So if we are
Mongo e,
database first time, we have to first create collection and
if the database actually exists. documents
After creating collections and documents, we can check whether
beforecreatweing,checya
already exist by doing the following operation: the database
o By listing the all the database in the system:
import pymongo
client = pymongo. MongoClient("mongodb
://localhost:27017/)
print(client.list_database_names())
When we execute the above code we will get the list of
available like, databases which are
C:\>Users\acer\Desktop\firstprogram. py
['admin', 'config', 'local', 'mydatabase ' \mydb']
C:\>
Or
We can check by specificdatabase name such as:
import pymongo
client = pymongo. MongoClient ('
mongodb://localhost:27017/'
dblist = client.list_ database _names ()
)
if "mydb" in dblist:
print ("The database exists.")
Output:
C:\>Users\acer\Desktop\firstprogram. py
The database exists.
C:\>_
4.6.2 Collections
A collection in
MongoDB is the same as a table in SQL databases.
Creating Collections:
To create a collection in MongoDB, use database object and specify the name ofthe
COlectuon you want to create. MongoDB will create the
Example: In the following example, we will create the collection
collection ifcalled
it does not e
"employee"
ímport pymongo
client =
pymongo.MongoClient ("mongodb://localhost : 27017/")
db = client ["mydb"]
mycol = db["employee" ]
In MongoDB, Collection is not Created until it gets the
MongoDB waits until you have inserted a document content. Creates
4.12
[MCA
rogramming
- - Sem. I) Python Database Interaction using MongoD8
tifCollectionexists:
MongoDB,a
collection is nnot created until it gets content, so if this is your first tirne
collection, we have to first create documents before we check if the
a a
reatingexists.
nolection
we can check whether the collection is already exist, by
creating documents
Atterthefollowing
operation:
daing
listing the all the collections in the system:
By Pymongo
import
client = pymongo.
MongoClient ('mongodb: //localhost :27e17/')
['mydb']
do = client"employee"]
nycol = db[
print(db.list collection_names())
Output:
C:\>Users\acer\Desktop\firstprogram. py
['example', 'employee']
C:\>_
0r collection name such as:
We can check by specific
import pymongo 'mongodb://l0calhost :27017/')
client = pymongo. MongoClient(
db = client['mydb']
collist = db.list collection_names()
if "employee" in collist:
print ("The collection exists. ")
Output:
C:\>Users\acer\Desktop\firstprogram.Py
The collection exists.
C:\>_
63 Documents representation of
documents. BSON is a binary
MongoDB
SON stores data records as BSON
more datatypes than JSON.
VCumentdocument s, ,though it contains
Structure: field-and-value pairs and have the
following
field1: valuel,
field2: value2
field3: value3
fieldN: valueN
4.13
Python Programming (MCA - Sem. I] Python Database
Interaction using
4.7 PERFORMINGCRUD OPERATIONS Mongo a,
CRUD Operationsare:
1. CCreate 2. R-Read
3. U-Update 4. D-Delete
Now we willgo through each operation one by one.
4.71C-Create
Mongo store the data in the form of JSON objects. So every record
for a
Mongo is called a document. If the collection does not
currently
operations will create the collection. We can insert the documents into exist, collectionsertn in
ways:
1. insert_one)
collection in 2
2. insert_many)
1. insert_one):
To insert a record, or document as it is called in
insert one() method. The first parameter of theMongoDB, into a collection, we use tha
containing the name(s) and value(s) of each field ininsert_one() method isa dictiona
the document you want to insert
Example: In the following example, we insert record in "employee" collection.
import pymongo
client = pymongo.MongoClient("
db = client['mydb']
mongodb://localhost:27017/')
mycol = db["employee" ]
mydict = { "name": "Rahul", "address": "Mumbai" }
x = mycol.insert_one (mydict)
print (x)
Output:
C:\>Users\acer\Desktop\firstprogram. py
<mymongo.results. InsertoneResult obiect at x00002BDO8F6CCCO>
C:\
The insert one() method returns a InsertOneResult object, which has a property.
inserted id, that holds the id of the inserted document.
2. insert_many() method:
To insert multiple documents into a collection in MongoDB, we use the insert many()
method. The first parameter of the method is a list
containing
insert_many()
dictionaries with the data you want to insert:
Example:
import pymongo
client = pymongo. Mongoclient
db = client ["mydb"] ("mongodb://localhost:27017/)
mycol = db["employee"]
mylist = [
4.14
[MCA - Sem.
I Python Database Interaction using MongoD8
Programming
.insert_many (mylist)
mycol.
the id values of the inserted documents:
X= list of
orint
(x.inserted ids)
#print
C:\>Userss\acer\Desktop\firstprogram.
Output: \ .py
[0bjectId(!605485a3d1a59c082be40db3'),
0bjectId(' 605485a3d1a59c082be40db4'
0bjectId('605485a3dla59c082be40dbs'),
0biectId('605485a3d1a59c082be40db6')1
4.16
(MCA.- Sem. I] Python Database Interaction using MongoDB
arogramming
C:\>
update many(): update_many()
all documents that meets the criteria of the query, use the
update
To
Example:Inthefollowing example,
method. we update allthe documents where address starts
theletter N'.
withimport Pymongo
.MongoClient("mongodb://localhost:27017/")
client = pymongo.
db = client ["mydb"]
mycol = db["employee"]
}
= { "address": { "$regex": "AN" } }
myquery "name": "Manisha" }
newvalues = {"$set": {
newvalues)
= mycol.update_many (myquery,
"documents updated. ")
Xprint (x.modified_count,
Output:
C:\>Users\acer\Desktop\firstprogram. py
2 documents updated.
C:\>
474 D-Delete methods:
documents in the collection using the following
recan delete the
1. delete_one)
2 delete_many)
of the
delete_one): one() method. The first parameter the
To we use the delete delete. If
delete one document, W defining which document to
delqueryete one() method is a query object only first occurrence is deleted. the address
finds the more than one document, delete the
document with
Exampl
"Nagpure:
".
In the following example, we
try:
orders_collection.update_one(
{'id': 1},
'Processed'}},
{'$set': {'status':
session=session
perations
are Committed as a single atomic operation.
4.19
Python Programming [MCA -Sem. ) Python Database
Interaction using
4.8.2 Rollback in MongoDB Using Python
Rollback in MongoDB refers to undoing the operations performed
Mongae
before committing. This can be achieved using the within a
error occurs during the transaction.
Example:
abortTransaction() mettranshodactiofnan
session = client.start_session()
session.start_transaction()
try:
db.users.update_one(
{'_id': 1},
{'$inc': {'balance': -50}},
session=session
)
db.payments.insert_one(
{'user_id': 1, 'amount': 50, 'status': 'Paid'},
session=session
)
# Simulate an error
raise Exception("Simulated error")
session.commit transaction()
except Exception as e:
session.abort_transaction()
print (f"Transaction aborted due to: {e}")
finally:
In
session.end_session()
the above example, if the
an error Occurs during the transaction,noneof
abort_transaction() method is
the operations are applied to
called to roll back the changes, ensuring that
the database.
4.8.3Cursor in MongoDB Using Python
Cursors in MongoDB are used to iterate over the results of a query. In Python,
results.
pymongo library provides cursor operations to retrieve and manipulate query
Basic Cursor Operations:
4.20
(MCA Sem. I] Python Database Interaction using MongoD8
Prgramming
query and returns a cursor to the matching documents.
os Executes a
find):Retrievesthe next document from the cursor.
1).limit(5)
in cursor:
for document
print (document)
collection for documents in the "Electronics"
products top 5.
This example queries the descending order, and limits the results to the
price in
category, sorts them by
using a loop.
he results are then printed
49 HANDLING ERRORS
mechanism to ensure that operations are
error-handling errors
MongoDB uses a robust appropriate measures are taken whennetwork
completed or that errors, such as
uier successfully mechanism covers various types of
ur. 1nis error-handling validation errors, and cursor errors.
crors, write errors, schema
91 Network Errors connection between the clhent
issues in the dropped
0cCur due to timeouts,
anderTors in MongoDB from transient issues like
and the server. These errors can result
raises exceptions such
Connections, inor unreachable servers.
PyMong0 library or NetworkTimeout when
Handling Python: The ConnectionFailure,
ServerSelectionTimeoutEr or,
Rtwork issues occur.
Example:
from Pymongo
import MongoClient, errors
try:
Client =MCongoClient("mongodb://localhost:27017/",
StrvenSelectionT TimeoutMS=5000)
4.21
Python Database
Python Programming (MCA -Sem. ) Interaction using
db = client.testdb
# Attempt to connect and perform a simple operation
Mongota,
db.list_collection_names()
excepterrors.ServerSelectionTimeoutError as e:
print(f"Network error: {e}")
excepterrors.ConnectionFailureas e:
print(f"Connection failed: {e}")
In the above example, if the MongoDB server is unreachable or the
out, a ServerSelectionTimeoutError orConnectionFailure will beconnection times
raised.,
the application to handle the situation appropriately, such as
or alerting the user. retrying the allowing
connection
4.9.2 Write Errors
Write errors occur when MongoDB fails towrite data to a
collection. This can hannen
due to various reasons such as duplicate keys,
exceeding document size limits. or
violating unique index constraints.
Handling in Python: PyMongo raises exceptions like
WriteError when write operations fail. DuplicateKeyError and
Example:
from pymongo import MongoClient, errors
client =
MongoClient("mongodb://localhost:
db = client.testdb
27017/")
try:
db.users.insert _one({"_id": 1, "name": "John"})
db.users.insert one({" id": 1, "name": "Jane"}) # This will cause a
DuplicatekeyError
excepterrors.DuplicatekKeyErroras e:
print(f"Write error: fe}")
The second insert_one operation in the example above will raise a DuplicatekeyError
because it attempts to insert a document with a
allows the application to notify the user or take duplicate id. Handing uus
4.9.3 Schema Validation Errors corrective action.
Schema validation errors occur when a document fails to meet the validation criteria
-based
specified for a collection. MongoDB allows you
to JSON Schema-
validation rules that documents must adhere to. define
the violates
Handling in Python: PyMongo raises OperationFailure when a doocument
schema validation rules during an insert or update operation.
4.22
Sem.I) Python Database tnteraction using Mongo8
IMCA-
collection("contacts" validator={
ry:
create
SjsonSchema":
to. f
hsonType": "object",
"required": ["phone"],
"oroperties": f
"ohone": f
"bsonType": "string, and is required"
: "must be a string
"description"
"validationAction": "error"
a string
will fail1 because "phone" is not
This insert "John" "phone": 12345})
do.contacts.insert one({"name":
exCepterrors.OperationFailureas e:
field
Print(f"Schema validation error:
{ef)
with a non-string"phone"
the
to insert a document valid data is stored in
hthe above example, attempting
ensuring that only
TeSults in an OperationFailure,
tol ection. related
S4 Cursor Errors These errors are often
result set. longer
is no
Cursor errors Can occur when iterating Over a
trying to access a
cursor that
0ssues like timeouts, or
available (e.g. invalid queries,
after a timeout or
disconnection).
like
CursorNotFound
Or
exceptions
Handl in g in
ExeCutionT imeoutPython:
when
PyMongo raises
there are 0ssues
with cursor
operations.
Etxroammppymongo
le: import Mongoclient, errors
4.23
Python Database
Python Programming (MCA -Sem. )
Interaction using
client = MongoClient("mongodb://localhost: 27017/")
db client.testdb
Mongobe,
try:
cursor = db.users.find({"age" : {"$gt": 25}}) .max_time ms(50a
for dOcument in cursor:
print (document)
excepterrors.ExecutionTimeoutas e:
print (f"Cursor error: {e}")
excepterrors.CursorNotFoundas e:
print (f"Cursor not found: fe}")
In this above example, if the query takes too long, it will raise an
exception. This allows the application to handle long-running queries ExecutionTimeout
for example, retrying the query with different parameters or notifying thegracefully by
user.
SUMMARY
> NoSQL is a non-relational DBMS that does not require a fixed
schema, avoids ioins
and is easy to scale.
The concept of NoSQL databases became popular with Internet giants
like Google.
Facebook, Amazon, etc. who deal with huge volumes of data.
> In the year 1998, Carlo Strozzi use the term
NoSQL for his lightweight, open-source
relational database.
NoSQL databases never follow the relational model it is either
schema-free or has
relaxed schemas.
Four types of NoSQL Database are: 1, Key-value Pair Based 2. Column-oriented
Graph 3. Graph- based 4. Document-oriented.
Data plays a crucial role in application development. It plays such a crucial role
that there are people called Database Administrators that specialize in just
organizing the data.
The traditional database used to be, and often still is. a
relational database.
MongoDB is more flexible than a relational database, and as such providesthe
developer with a database that can easily be changed during the development
phase.
Pythonhas
The increased popularity of MongoDB popularity of APIfor
resulted in a Python library called combined with the provides an
communicating with MongoDB.
PyMongo, which
4.24
Python Database
Python Programming (MCA -Sem. I) Interaction using
PRACTICE QUESTIONS Mongnta
QIAnswer the following questions in short.
1. Which are the types of NoSQL databases?
2. Write examples of key-value store Databases.
3. Name the fields where Column-based NoSQL databases are widely used
4. Which are popular graph-based databases?
5. What is a Sharding?
Q.II Answer the following questions.
1. Explain NoSQL database.
2. List and explain the advantages of NoSQL database.
3. Differentiate between SQL and NoSQL.
4. Explain the features of MongoDB.
5. Explain installation steps of PyMongo.
6. Explain Collection,database and documents in PyMongo.
7. Explain the CRUD operations of MongoDB with Python.
Q.III Write a short note on:
1. Graph-based databases
2. Column-based NoSQL databases
3. Features of MongoDB
4. C-Create CRUD operation
5. Types of NoSQL Databases
4.26