Intro.
NoSQL is a term that describes a range of database technologies developed to address the limitations of traditional (RDBMS). Traditional RDBMS, like those using SQL organize data into fixed tables, rows, and columns, which can be restrictive for certain types of data and applications.
Types of NoSQL databases:
1. Key-Value Stores:
What Are They? Think of a key-value store as a very simple filing system. Each piece of data ("value") is stored with a unique name ("key"), just like a file stored in a folder with a label.
Example: Imagine a locker system. Each locker (key) holds your belongings (value). When you need something, you go straight to the locker using its number.
Use: Great for situations where you need to retrieve data quickly and don't care about its structure, like session information in a website.
2. Document Stores:
What Are They? These are like enhanced key-value stores. Instead of just storing a simple item, they store complex items (documents), like a filled-out form with various types of information.
Example: Think of a detailed profile on a social media site. Your profile (document) contains your name, photos, posts, etc., all stored together.
Use: Useful for storing and managing more complex data like user profiles, blog posts, etc.
3. Wide-Column Stores:
What Are They? Imagine a huge table where each row can have different columns. Unlike regular tables (as in RDBMS) where every row has the same columns, here, rows can have various columns suited to their specific needs.
Example: In a store's inventory system, one item (row) might have size, color, and price, while another item might have weight, material, and price.
Use: Ideal for managing large datasets where different data points can have different attributes.
4. Graph Stores:
What Are They? These databases focus on relationships. They store data points (like people, places, things) and the connections between them.
Example: In a social network, you (a data point) are connected to friends (other data points), and the database tracks how everyone is interconnected.
Use: Perfect for scenarios where the relationship between data points is crucial, like social networks, recommendation systems, or network analysis.

Mongo Shell Basic Commands
Basic commands:
Examples of CRUD (Create, Read, Update, Delete) operations in the MongoDB shell:
show dbs // shows all available databases
# count all documents in a specific collection
db.myCollection.countDocuments({})Create Operations
There are two main methods for creating documents in MongoDB: insertOne() and insertMany().
insertOne(): This method inserts a single document into a collection. Here's an example:
db.collection.insertOne({
field1: "value1",
field2: "value2"
});insertMany(): This method inserts multiple documents into a collection. Here's an example:
db.collection.insertMany([
{field1: "value1", field2: "value2"},
{field1: "value3", field2: "value4"}
]);Read Operations
find(): This method retrieves all documents in a collection. Here's an example:
db.collection.find();findOne(): This method retrieves a single document from a collection. Here's an example:
db.collection.findOne({field1: "value1"});Update Operations
updateOne(): This method updates a single document in a collection. Here's an example:
db.collection.updateOne(
{field1: "value1"}, // filter
{$set: {field2: "newValue"}} // update
);updateMany(): This method updates all documents that match the filter. Here's an example:
db.collection.updateMany(
{field1: "value1"}, // filter
{$set: {field2: "newValue"}} // update
);Delete Operations
deleteOne(): This method deletes a single document from a collection. Here's an example:
db.collection.deleteOne({field1: "value1"});deleteMany(): This method deletes all documents that match the filter. Here's an example:
db.collection.deleteMany({field1: "value1"});MongoDB's Python driver:
Adding a new document to a collection.
insert_one:
insert_one is a method used in MongoDB's Python driver, PyMongo
This method is preferred when you only need to insert one document at a time, as opposed to
insert_many, which is used for inserting multiple documents in one operation.When
insert_oneis called, it not only inserts the document but also returns anInsertOneResultobject, which contains information about the operation.
inserted_id
inserted_idis an attribute of theInsertOneResultobject returned by theinsert_onemethod.It holds the
_idvalue of the newly inserted document.The
_idfield is a unique identifier for each document in a MongoDB collection. If_idis not explicitly specified in the document being inserted, MongoDB automatically generates a unique ObjectId for it.
update_one, update_many, or replace_one.
In PyMongo, which is the Python driver for MongoDB, updating documents in a collection is achieved using methods like update_one, update_many, or replace_one. These methods allow you to modify documents in a MongoDB collection.
Here's an example of how to use these methods in PyMongo:
from pymongo import MongoClient
# Connect to the MongoDB server
client = MongoClient('localhost', 27017)
# Select the database and collection
db = client.my_database
collection = db.my_collection
# Update one document
collection.update_one({"name": "Alice"}, {"$set": {"age": 31}})
# Update multiple documents
collection.update_many({"name": "Bob"}, {"$set": {"age": 29}})
# Replace one document
collection.replace_one({"name": "Charlie"}, {"name": "Charlie", "age": 32})
Basic Usage of find
Here's a basic structure to find documents in a collection:
Basic Usage of find
Here's a basic structure to find documents in a collection:
python
Copy code
from pymongo import MongoClient
# Connect to MongoDB
client = MongoClient('mongodb://localhost:27017/')
db = client['my_database'] # Replace with your database name
collection = db['my_collection'] # Replace with your collection name
# Find all documents
documents = collection.find()
# Iterate through the documents and print them
for doc in documents:
print(doc)Note:
ORM (Object-Relational Mapping): A technique for mapping database tables to objects in a programming language. Used with relational databases (like MySQL, PostgreSQL). Simplifies database access by letting developers use their language's syntax instead of SQL.
ODM (Object-Document Mapping): Similar to ORM, but for NoSQL, document-based databases (like MongoDB). Maps collections and documents in the database to objects and attributes in the programming language. Allows for easy interaction with the database using native language constructs.
Use
updateManyif you are working with MongoDB 3.2 or newer. It's more modern, clearer, and provides better feedback about the operation.BUT Use the
updatemethod with{ multi: true }if you are working with a MongoDB version older than 3.2 or in situations where you need to maintain compatibility with older codebases.
Resources:
https://www.digitalocean.com/community/tutorials/how-to-use-the-mongodb-shell
https://www.digitalocean.com/community/tutorials/how-to-use-mongodb-in-a-flask-application




