Mongoose

Mongoose is the de facto standard ODM (Object Document Mapper) for using MongoDB in Node.js environments. It provides powerful schema definitions, validation, and middleware capabilities, enabling structured data management while maintaining MongoDB's flexibility.

ORMJavaScriptMongoDBNoSQLDocument-orientedSchema

GitHub Overview

Automattic/mongoose

MongoDB object modeling designed to work in an asynchronous environment.

Stars27,287
Watchers481
Forks3,859
Created:April 6, 2010
Language:JavaScript
License:MIT License

Topics

mongomongodbnodejsodmorm

Star History

Automattic/mongoose Star History
Data as of: 7/19/2025, 08:07 AM

Library

Mongoose

Elegant MongoDB object modeling library. Schema-based solution for modeling MongoDB documents. Standard combination for Node.js and MongoDB.

Overview

Mongoose is the de facto standard ODM (Object Document Mapper) for using MongoDB in Node.js environments. It provides powerful schema definitions, validation, and middleware capabilities, enabling structured data management while maintaining MongoDB's flexibility.

Key Features

  • Schema-based: Manage document structure with clear schema definitions
  • Validation: Built-in and custom validation rules
  • Middleware: Insert processing with pre/post hooks
  • Virtual Properties: Computed properties and getters/setters
  • Plugin System: Implementation of reusable functionality

Usage Examples

Basic Usage

const mongoose = require('mongoose');

// Schema definition
const userSchema = new mongoose.Schema({
  name: {
    type: String,
    required: true
  },
  email: {
    type: String,
    required: true,
    unique: true,
    lowercase: true
  },
  age: {
    type: Number,
    min: 0
  },
  createdAt: {
    type: Date,
    default: Date.now
  }
});

// Model creation
const User = mongoose.model('User', userSchema);

// Data creation
const newUser = await User.create({
  name: 'John Doe',
  email: '[email protected]',
  age: 25
});

// Data query
const users = await User.find({ age: { $gte: 18 } });

Using Middleware

// Pre-save processing
userSchema.pre('save', async function(next) {
  if (this.isModified('password')) {
    this.password = await bcrypt.hash(this.password, 10);
  }
  next();
});

// Virtual property
userSchema.virtual('fullName').get(function() {
  return `${this.firstName} ${this.lastName}`;
});

Latest Trends (2025)

Maintains stable popularity as the standard choice for MongoDB development. Widely adopted in NoSQL application development with an unshakeable position in the MongoDB ecosystem. TypeScript support has been enhanced for improved type safety.

Pros and Cons

Pros

  • Standard tool for MongoDB development
  • Rich documentation and community
  • Powerful schema and validation features
  • Extensive plugin ecosystem

Cons

  • MongoDB-specific, no support for RDBs
  • Schema partially limits MongoDB's flexibility
  • Performance challenges with large datasets

Use Cases

  • Majority of Node.js + MongoDB projects
  • RESTful API servers
  • Real-time applications
  • Content management systems