WEB Programming
08-MongoDB

MongoDB and Mongoose Notes

Introduction to MongoDB

  1. Definition: MongoDB is an open-source NoSQL database that stores data in JSON-like documents (BSON).

  1. NoSQL vs SQL:
    • NoSQL: Developed in late 2000s, focuses on scaling, fast queries, and flexible schemas.
    • SQL: Developed in 1970s, focuses on reducing data duplication, has rigid schemas.

  1. Benefits of MongoDB:
    • Flexible data models
    • Horizontal scaling
    • Fast queries
    • Developer-friendly

  1. Terminology Comparison:
    RDBMSMongoDB
    TableCollection
    RecordDocument
    ColumnField
    JoinsEmbedded data

  1. MongoDB Atlas: Cloud-based database service for MongoDB.

Mongoose ODM

  1. Definition: Mongoose is an Object Data Modeling (ODM) library for MongoDB and Node.js.

  1. Installation and Setup:
    npm install mongoose
    To use Mongoose in your code:
    const mongoose = require('mongoose');

  1. Schema: Defines the structure of documents in a collection.

    Example:

    const companySchema = new Schema({
      companyName: { type: String, unique: true },
      address: String,
      phone: String,
      employeeCount: { type: Number, default: 0 },
      country: String
    });

  1. Model: Wrapper for the schema that provides an interface to the database.

    let Company = mongoose.model('companies', companySchema);

  1. Connecting to MongoDB:
    mongoose.connect('Your connection string here');

  1. Unique Index: Enforces unique values for specific fields.

    Example:

    const userSchema = new Schema({
      username: { type: String, unique: true },
      email: String
    });

    This ensures that no two documents can have the same username.


  1. Recursive Schema: Allows nested structures of the same schema.

    Example:

    const commentSchema = new Schema({
      text: String,
      author: String,
      date: { type: Date, default: Date.now }
    });
    commentSchema.add({ replies: [commentSchema] });

    This allows comments to have nested replies.


  1. Multiple Connections: Possible to connect to multiple databases.

    Example:

    const conn1 = mongoose.createConnection('mongodb://localhost/db1');
    const conn2 = mongoose.createConnection('mongodb://localhost/db2');
     
    const Model1 = conn1.model('Model1', someSchema);
    const Model2 = conn2.model('Model2', anotherSchema);

    This allows you to work with two different databases simultaneously.


CRUD Operations

  1. Create (Save):
    const newCompany = new Company({ ... });
    newCompany.save()
      .then(() => console.log("Saved"))
      .catch(err => console.log(err));

  1. Read (Find):

    Company.find({ companyName: 'The Kwik-E-Mart' })
      .exec()
      .then(companies => console.log(companies))
      .catch(err => console.log(err));
    • Can select specific fields: Company.find({}, 'address phone')

  1. Update:
    Company.updateOne({ companyName: 'The Kwik-E-Mart' }, { $set: { employeeCount: 3 } })
      .exec()
      .then(() => console.log('Updated'))
      .catch(err => console.log(err));

  1. Delete:
    Company.deleteOne({ companyName: 'The Kwik-E-Mart' })
      .exec()
      .then(() => console.log('Deleted'))
      .catch(err => console.log(err));

Best Practices

  1. Use .exec() after queries to return a proper promise.

  1. Encode passwords with special characters using encodeURIComponent().

    Example:

    const password = "p@ssw0rd!";
    const encodedPassword = encodeURIComponent(password);
    const connectionString = `mongodb+srv://username:${encodedPassword}@cluster0.example.net/mydb`;
    mongoose.connect(connectionString);

    This ensures that special characters in the password don't interfere with the connection string.


  1. Handle errors properly in all database operations.

  1. Use appropriate update operators like $set, $push, and $addToSet.

    Definition: Update operators are special keys that you can use to modify data in specific ways.

    Examples:

    • $set: Sets the value of a field.

      Company.updateOne({ name: "Acme Inc" }, { $set: { employees: 100 } });
    • $push: Adds an element to an array field.

      User.updateOne({ username: "john" }, { $push: { hobbies: "painting" } });
    • $addToSet: Adds an element to an array field only if it doesn't already exist.

      User.updateOne({ username: "john" }, { $addToSet: { tags: "developer" } });