Chapter 5
Chapter 5
Features of NoSQL
● Schema Flexibility: No fixed schema; easily handles unstructured and semi-structured data.
● High Performance: Optimized for fast data retrieval and handling large volumes.
● Varied Data Models: Supports document, key-value, column-family, and graph databases.
● Distributed Architecture: Data is spread across multiple nodes for reliability and availability.
2. Key-Value (e.g., Redis): Pairs of keys and values for fast lookups.
3. Column-Family (e.g., Cassandra): Data organized into columns for big data workloads.
Key: user:12345
Value: {"name": "foo bar", "email": "[email protected]", "designation": "software developer"}
● MongoDB stores data in JSON-like documents, which makes the database very flexible and scalable.
PyMongo
PyMongo
● To install PyMongo ,first make sure you have installed python3(along with PIP) an MongoDB properly then
execute following command.
pip install pymongo
● After installation successfully, we now verify weather installation is successful, for the that we open the new
document and write the following code and save as “firstprogram.py”.
import pymongo
● if you execute the firstprogram.py as shown below, you should not get any issues.
C:\User\acer\Desktop\firstprogram.py
C:\>_
client = pymongo.MongoClient(“mongo://localhost:27017”)
mydb = client[“mydb”]
Collections:
Example:
import pymongo
client = pymongo.MongoClient(“mongo://localhost:27017”)
mydb = client[“mydb”]
mycol = db[“employee”]
Document structure:
{
field1: value1,
field2: value2,
…..
fieldN: value3,
}
1. find()
Example:
import pymongo
client = pymongo.MongoClient(“mongo://localhost:27017”)
db = client[“mydb”]
mycol = db[“employee”]
for x in mycol.find():
print(x)
# Creating a client
client = MongoClient('localhost', 27017)
● We can easily filter the documents using the query object. To apply the filter on the collection,
● we can pass the query specifying the condition for the required documents as a parameter to this method,
Query Selectors: Following is the list of some operators used in the queries in MongoDB
client = pymongo.MongoClient("mongodb://localhost:27017/")
db = client["mydatabase"]
col = db["gfg"]