Creating a comprehensive MongooseJS cheat sheet involves compiling various commands and methods commonly used in Mongoose, a MongoDB object modeling tool designed to work in an asynchronous environment. Here’s a basic outline for a MongooseJS cheat sheet:

1. Setting Up and Connecting to MongoDB

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

2. Defining a Schema

const Schema = mongoose.Schema;
const exampleSchema = new Schema({
  name: String,
  age: Number,
  // ... other fields
});

3. Creating a Model

const Example = mongoose.model('Example', exampleSchema);

4. Creating a Document

const exampleDocument = new Example({ name: 'John', age: 30 });
exampleDocument.save();

5. Reading Documents

  • Find one: Example.findOne({ name: 'John' })

  • Find all: Example.find()

  • Find with condition: Example.find({ age: { $gt: 20 } })

6. Updating Documents

  • Update one: Example.updateOne({ name: 'John' }, { age: 31 })

  • Update many: Example.updateMany({ age: { $lt: 30 } }, { age: 30 })

7. Deleting Documents

  • Delete one: Example.deleteOne({ name: 'John' })

  • Delete many: Example.deleteMany({ age: { $gt: 50 } })

8. Working with Relations (Populate)

// Assuming anotherModel is related to Example
Example.find().populate('anotherModel');

9. Middleware (Pre and Post Hooks)

exampleSchema.pre('save', function(next) {
  // ... actions before saving
  next();
});

10. Indexing

exampleSchema.index({ name: 1 });

11. Virtuals

exampleSchema.virtual('nameAge').get(function() {
  return this.name + ' ' + this.age;
});

12. Error Handling

exampleDocument.save(function(err) {
  if (err) console.error(err);
});

13. Additional Options

  • Schema options: timestamps, typeKey, etc.

  • Query helpers

  • Static methods


This cheat sheet covers fundamental aspects of working with MongooseJS. It includes basic operations like connecting to a database, CRUD operations, and advanced features like virtuals and middleware. For a more comprehensive guide, you might want to refer to the official Mongoose documentation.