Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

2.2 Key-Value and Document Data Models

Lesson 11 of 36 in the free Big Data-1 notes on Siksha Sarovar, written by Rohit Jangra.

2.2.1 Key-Value Databases

Key-Value stores are the simplest NoSQL data models. Every item is stored as an attribute name (key) together with its value.

  • Key: A unique identifier (e.g., "user:123").
  • Value: Opaque to the database—it can be text, a blob, or an object.
  • Best for: Session data, user profiles, shopping carts, and caching.
  • Examples: Redis, Riak, Amazon DynamoDB.

2.2.2 Document Databases

Document databases are similar to Key-Value stores, but the value is "transparent" to the database. It is typically stored in a format like JSON or BSON.

  • Transparency: Because the DB understands the document structure, you can query specific fields (e.g., "Find all users where city = 'Mumbai'").
  • Nested Structures: Documents can contain arrays and sub-documents, allowing for complex data representation.
  • Examples: MongoDB, Couchbase.

2.2.3 Deep Dive into BSON (Binary JSON)

While JSON is human-readable, it is inefficient for computers to parse. MongoDB uses BSON.

  • Speed: BSON is designed specifically for fast traversal (it includes the length of fields so the DB can "skip" them without reading every byte).
  • Data Types: Unlike JSON, BSON includes native support for Dates, Binary data (images/files), and floating-point numbers.

2.2.4 Indexing in Document Databases

To avoid "Collection Scans" (reading every document), we use Indexes.

  1. Compound Indexes: Indexing multiple fields (e.g., { city: 1, age: -1 }). Useful for complex queries.
  2. Geospatial Indexes: Allowing for "Find the nearest coffee shop" queries based on latitude and longitude.
  3. TTL (Time-To-Live) Indexes: Automatically deleting documents after a certain period (perfect for logs or session data).

Comparison: Key-Value vs. Document

AspectKey-ValueDocument
QueryingOnly by primary Key.By any field within the document.
VisibilityValue is a "Black Box" to the DB.DB can "look inside" the document.
ComplexityExtremely simple and fast.More complex, allows sophisticated indexing.
Best Use CaseContent caching / Sessions.Dynamic web content / CMS.

2.2.3 Handling Relationships in NoSQL

NoSQL doesn't have "Foreign Keys" in the traditional sense. Relationships are handled in two ways:

  1. Embedding: Putting the related data inside the aggregate (e.g., putting 'Tags' inside a 'Post' document).
  2. Linking: Storing an ID that refers to another document manually (Programmatic join).