CHAP1 no sql database_085309
CHAP1 no sql database_085309
{
"employees":[
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter", "lastName":"Jones"}
]
}
Availability
o In the context of databases, availability refers to the
system's ability to remain operational and accessible, even
during failures or high demand.
o In NoSQL databases, availability is often prioritized over
consistency in distributed systems
o MongoDB uses replication and sharding to ensure high
availability.
o Replication involves copying data from one database
server to multiple servers. This ensures that the system can
continue to operate even if one server fails.
In MongoDB, replication is achieved through replica sets,
where one server (primary) accepts write operations, and
multiple secondary servers maintain copies of the data.
If the primary server fails, one of the secondaries can
automatically take over as the new primary, ensuring no
downtime.
Sharding is a horizontal scaling method that divides large
datasets across multiple servers, or shards.
o Each shard contains a portion of the data, allowing the
system to handle massive amounts of data and traffic by
distributing the load.
o Sharding is particularly useful when a single server can no
longer store or process all the data due to resource
limitations (e.g., CPU, memory, storage).
Example: In MongoDB, a collection can be divided
(sharded) across multiple servers based on a shard key (a
field in the document).
For instance, a large e-commerce dataset could be sharded
across three servers based on a customerID , field, where
each shard holds a subset of customers.
[ Shard1 ] [ Shard2 ] [ Shard 3 ]
customerID 1-100K customerID 100K-200K customerID 200K-300K
Documents
o In MongoDB, a document is the basic unit of data, similar
to a row in a relational database.
o Documents are represented in a JSON-like format (BSON in
MongoDB, a binary version of JSON).
What is BSON?
BSON stands for Binary JSON. It is a binary-encoded
serialization format used to store and transport data.
MongoDB, a NoSQL database, uses BSON to store
documents on disk and transfer data over the network.
o Each document can store key-value pairs, and the structure
of each document can vary within the same collection.
Example:
{
"_id": "123",
"name": "Alice",
"age": 25,
"address": {
"street": "123 Main St",
"city": "New York"
}
}
Collection
o A collection is the equivalent of a table in a relational
database system.
o It is a group of documents. Unlike a table, there is no
enforced schema, so documents in a collection can have
different structures.
o Collections organize documents into logical groups for
querying and managing.
Example: A collection called users might store documents
representing individual user data.
Indexing
o Indexing in MongoDB helps to improve query
performance.
o It allows the database to quickly locate documents in a
collection by creating special data structures (indexes) that
store a small portion of the document's data in an efficient
way.
o Without indexes, MongoDB must scan every document in
a collection, leading to slow query performance.
Example: An index on the name field in the users collection
will speed up queries searching for users by name.
Optimistic Locking
o Optimistic locking is a concurrency control mechanism
where a system assumes that multiple transactions can
complete without affecting each other.
o In MongoDB, optimistic locking can be implemented using
a field such as a version number (v field) that is
incremented with every document update.
o If two updates attempt to modify the same document at the
same time, one will fail, ensuring that no conflicting
changes are applied.
o Use Case: Optimistic locking is useful in distributed
systems where locking mechanisms should be avoided to
maintain high performance and availability.
Relationships
o In NoSQL databases like MongoDB, relationships between
data are not as strictly enforced as in relational databases.
However, relationships can still be modeled.
o There are two types of relationships:
Embedding: Storing related data in the same document.
Referencing: Storing related data in separate
documents, using references (similar to foreign keys in
relational databases).
o Example: An order document may embed product details
directly, or it may reference products stored in a separate
collection.
Data Model
o The data model defines how data is structured, stored, and
accessed in a database.
o In MongoDB, the data model is flexible and can adapt to
the application's needs. Unlike rigid schemas in SQL
databases, MongoDB’s data model allows for varied
document structures within a collection.
o Example: In MongoDB, one user document might have
fields like email, address and phone, while another user
document might have only an email and phone
Schema
o A schema defines the structure of data.
o In traditional relational databases, schemas are strict,
defining the fields and data types for each table.
o In MongoDB, schema is flexible, meaning there is no
enforced structure by default, but schema validation can be
added if needed to impose constraints on the data.
o Example: MongoDB allows documents in the same
collection to have different fields, but you can enforce
schema validation rules using JSON Schema.
Mongosh
o Mongosh is the MongoDB shell, a command-line interface
for interacting with MongoDB databases.
o It allows users to execute queries, manipulate data, and
perform administrative tasks directly from the terminal.
o Mongosh provides a JavaScript-based environment to work
with MongoDB.
o Usage: You can use Mongosh to connect to a MongoDB
instance, insert or query documents, and perform database
management tasks like creating indexes or viewing
collection statistics.
1.Characteristics of Collections in MongoDB
Schema-Less Nature:
o Unlike traditional relational databases, MongoDB
collections don’t enforce a fixed schema. Each document
can have different fields and structures.
Document Storage:
o Documents are stored inside collections. Each document
represents a piece of data.
Explicit Creation:
o If a collection does not exist, MongoDB creates it
automatically when you first store data in that collection.
Document Validation:
o By default, MongoDB collections do not enforce a strict
schema.
o However, you can set up document validation rules
during update and insert operations.
o Schema validation ensures that documents meet specific
criteria (e.g., required fields, data types) within a collection.
Unique Identifiers:
o Each collection is assigned an immutable UUID
(Universally Unique Identifier).
o The collection UUID remains the same across all
members of a replica set and shards in a sharded cluster.
Sharding and Replication:
o Collections can be sharded (distributed across multiple
servers) and replicated (duplicated across servers) to
ensure high availability and scalability.
2. Features of NoSQL Databases
Distributed Architecture:
o NoSQL databases distribute data across nodes, allowing
them to handle massive amounts of information. This
architecture enhances fault tolerance and resilience.
Varied Data Models:
o They support different data models, including key-value
pairs, documents, wide-columns, and graphs, catering to
various types of data and use cases.
Types of NoSQL Databases
NoSQL databases are categorized based on their data
models:
Document Stores:
o Store data in document format, usually JSON or BSON.
Each document is a self-contained unit of data.
o Examples: MongoDB, CouchDB
o Use Cases: Content management systems, real-time
analytics, user profiles
Key-Value Stores:
o Store data as a collection of key-value pairs. Keys are
unique identifiers for the data, and values can be strings,
numbers, or more complex data structures.
o Examples: Redis, Amazon DynamoDB
o Use Cases: Caching, session management, simple
lookup operations
Column-Family Stores:
o Store data in columns rather than rows.
o Data is organized into column families, which group related columns.
o Examples: Apache Cassandra, HBase
o Use Cases: Big data analytics, time-series data, write-heavy
applications
Graph Databases:
o Store data in graph structures,
o Efficient for navigating complex relationships.
o Represent data as nodes, edges, and properties.
o Useful for social networks, recommendation engines, and
fraud detection.
o Examples: Neo4j, ArangoDB
o Use Cases: Social networks, recommendation engines, fraud
detection
4. Data Types in NoSQL Databases
Q11.How do you create a new user in MongoDB using the MongoDB Shell?
Ans:You can create a new user in MongoDB using the MongoDB Shell by running
the command db.createUser({ user: "myuser", pwd: "mypassword", roles:
["readWrite"] })
Q12. What is the purpose of the mongo shell in MongoDB?
Ans:The mongo shell is a command-line interface for interacting with MongoDB
instances.
Q13.How do you perform data modeling in MongoDB?
Ans:You can perform data modeling in MongoDB by defining a data model that
represents the structure and relationships of the data.