Episode 11 :Oplog Aur MongoDB Mein Database Creation Ka Jaadu

 

Oplog Aur MongoDB Mein Database Creation Ka Jaadu

1. Oplog (Operational Log) Ka Jaadu:

Definition:

  • Oplog (Operational Log): MongoDB ka oplog ek special capped collection hai jo primary node ke saare write operations ko record karta hai. Yeh log replica set ke members ke beech synchronization aur replication ke liye use hota hai.

Key Points:

  • Purpose: Oplog ka main purpose replica set members ko primary node ke updates ke saath synchronize karna hai. Jab primary node par koi write operation hota hai, woh oplog mein record hota hai, aur secondary nodes woh operations apply karke apne data ko update karte hain.

  • Capped Collection: Oplog ek capped collection hai, matlab isme fixed size hota hai aur purane entries automatically delete ho jati hain jab new entries add hoti hain.

  • Structure: Oplog ek document-based log hota hai, jo operations ko timestamp ke saath store karta hai. Har operation ka ek unique oplog entry hota hai, jo replication aur failover ke liye use hota hai.

Example:

  • Reading Oplog:
    javascript
    // Connect to the admin database use admin; // View oplog entries db.oplog.rs.find().limit(5);

2. MongoDB Mein Database Creation:

Database Creation:

  • MongoDB mein database creation dynamic hoti hai. Agar aap ek collection create karte hain aur usme data insert karte hain, to MongoDB apne aap ek new database create kar deta hai.

Steps:

  1. Connect to MongoDB:

    bash

    mongo
  2. Create a Database:

    javascript

    // Switch to a new database (it will be created if it doesn't exist) use myNewDatabase;
  3. Create a Collection:

    javascript

    // Create a new collection within the database db.myCollection.insert({ name: "Alice", age: 30 });

    Note: Database will not be physically created on disk until it contains data (i.e., when you insert documents into at least one collection).

  4. Verify Database Creation:

    javascript
    // List all databases show dbs; // Check if the database is listed use myNewDatabase; db.myCollection.find();

    Example Command:

    javascript
    // Insert a document into the collection db.myCollection.insert({ name: "Bob", age: 25 });

    Verification:

    javascript
    // Check if the database and collection exist
    use myNewDatabase; db.myCollection.find().pretty();

Summary:

  • Oplog: MongoDB ke oplog mein primary node ke saare write operations record hote hain, jo replication aur synchronization ke liye use hote hain.
  • Database Creation: MongoDB mein database creation dynamically hoti hai, jisme aapko pehle ek collection create karna hota hai aur data insert karna hota hai. Phir database physically create hota hai.

Post a Comment

0 Comments