# ORM (Object Relational Mapping)

#### What is ORM?

**ORM (Object-Relational Mapping)** is a technique that allows developers to interact with a database using an object-oriented programming approach instead of writing raw SQL queries. ORMs map database tables to classes in a programming language, and table rows to instances of those classes. This abstraction layer simplifies database interactions and promotes cleaner, more maintainable code.

#### Mongoose with and without ORM

**Mongoose** is a popular ODM (Object-Document Mapper) for MongoDB and Node.js. Since MongoDB is a NoSQL database and does not use traditional SQL, Mongoose functions more as an ODM rather than ORM. However, for the sake of understanding, we can compare the two approaches:

1. **Using Mongoose (with ODM):**
   * Mongoose provides a schema-based solution to model your application data.
   * It simplifies data validation, casting, and business logic enforcement.
   * You interact with the database using Mongoose models, which represent your collections.
2. **Using MongoDB without Mongoose (without ORM/ODM):**
   * You directly interact with the MongoDB database using the native MongoDB driver.
   * This approach requires manual handling of data validation, schemas, and business logic.
   * It often involves writing more boilerplate code and managing raw database queries.

#### Example

Let's compare an example of defining and interacting with a `User` collection in MongoDB using both approaches.

**With Mongoose (using ODM)**

1. **Setup and Define Schema:**

   ```javascript
   const mongoose = require('mongoose');
   mongoose.connect('mongodb://localhost/mydatabase', { useNewUrlParser: true, useUnifiedTopology: true });

   const userSchema = new mongoose.Schema({
     name: { type: String, required: true },
     email: { type: String, required: true, unique: true },
     age: Number
   });

   const User = mongoose.model('User', userSchema);
   ```
2. **Create and Save a User:**

   ```javascript
   const newUser = new User({ name: 'John Doe', email: 'john.doe@example.com', age: 30 });

   newUser.save()
     .then(user => console.log('User saved:', user))
     .catch(err => console.error('Error saving user:', err));
   ```
3. **Find Users:**

   ```javascript
   User.find({ age: { $gte: 18 } })
     .then(users => console.log('Users found:', users))
     .catch(err => console.error('Error finding users:', err));
   ```

**Without Mongoose (using native MongoDB driver)**

1. **Setup and Connect:**

   ```javascript
   const { MongoClient } = require('mongodb');
   const url = 'mongodb://localhost:27017';
   const dbName = 'mydatabase';

   MongoClient.connect(url, { useNewUrlParser: true, useUnifiedTopology: true }, (err, client) => {
     if (err) throw err;
     const db = client.db(dbName);
     const usersCollection = db.collection('users');

     // Example operations can go here
   });
   ```
2. **Create and Save a User:**

   ```javascript
   const newUser = { name: 'John Doe', email: 'john.doe@example.com', age: 30 };

   usersCollection.insertOne(newUser, (err, result) => {
     if (err) throw err;
     console.log('User saved:', result.ops[0]);
   });
   ```
3. **Find Users:**

   ```javascript
   javascriptCopy codeusersCollection.find({ age: { $gte: 18 } }).toArray((err, users) => {
     if (err) throw err;
     console.log('Users found:', users);
   });
   ```

#### Key Differences

1. **Schema Definition:**
   * **Mongoose:** Uses a schema-based approach. You define schemas and models which enforce structure and validation.
   * **Native Driver:** No built-in schema enforcement. You handle the data structure and validation manually.
2. **Data Validation:**
   * **Mongoose:** Automatic validation based on the schema before saving or updating documents.
   * **Native Driver:** Manual validation. You need to write your own validation logic.
3. **Code Simplicity:**
   * **Mongoose:** More concise and readable. Provides higher-level abstractions for common operations.
   * **Native Driver:** More verbose. Requires handling low-level operations directly.
4. **Community and Plugins:**
   * **Mongoose:** Has a rich ecosystem with plugins for additional functionality like pagination, soft delete, etc.
   * **Native Driver:** Fewer third-party plugins available, but more flexibility for custom implementations.

Using Mongoose can significantly simplify and streamline database interactions, especially for applications requiring complex data models and relationships. However, using the native MongoDB driver offers more control and flexibility, which might be preferred for certain types of applications.
