Skip to content

Instantly share code, notes, and snippets.

@tanvirstreame
Forked from subfuzion/mongoose-cheatsheet.md
Created January 18, 2023 09:32
Show Gist options
  • Save tanvirstreame/4eb6931d0da202cf855b35f24c700a9f to your computer and use it in GitHub Desktop.
Save tanvirstreame/4eb6931d0da202cf855b35f24c700a9f to your computer and use it in GitHub Desktop.
mongoose cheatsheet

Definitely not comprehensive. This is meant to be a basic memory aid with links to get more details. I'll add to it over time.

Install

$ npm install mongoose --save

Connect

const mongoose = require('mongoose');

const uri = process.env.MONGO_URI || 'mongodb://localhost/test';

mongoose.connect(uri, function(err, res) {
  ...
});

Defining a schema

const userSchema = new mongoose.Schema({
  name: {
    first: String,
    last: { type: String, trim: true }
  },
  age: { type: Number, min: 0 },
  posts: [ { title: String, url: String, date: Date } ],
  updated: { type: Date, default: Date.now }
});
SchemaTypes
  • String
  • Number
  • Date
  • Buffer
  • Boolean
  • Mixed
  • ObjectId
  • Array

SchemaTypes
SchemaType API
SchemaType#default
SchemaType#validate
get
set
select

Notes

Mixed types and dates that are modified using JavaScript Date methods are not hooked into mongoose change tracking logic. To save changes, let mongoose know about them using markModified('path') before calling save.

Instantiating a model

A model is a constructor compiled from a schema. Model instances represent documents.

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

var u = new User({
  name: {
    first: 'Tony',
    last: 'Pujals'
  },
  age: 99
});

Query

query

$where
query.$where('this.comments.length > 10 || this.name.length > 5')

// or

query.$where(function() {
  return this.comments.length > 10 || this.name.length > 5;
});

mongoDB $where

$all

mongoose $all
mongoDB $all

$count

http://mongoosejs.com/docs/api.html#query_Query-count

$distinct

http://mongoosejs.com/docs/api.html#query_Query-distinct

#elemMatch

http://mongoosejs.com/docs/api.html#query_Query-elemMatch

#equals

http://mongoosejs.com/docs/api.html#query_Query-equals

#exec

http://mongoosejs.com/docs/api.html#query_Query-exec

#exists

http://mongoosejs.com/docs/api.html#query_Query-exists

#find

http://mongoosejs.com/docs/api.html#query_Query-find

findOne

http://mongoosejs.com/docs/api.html#query_Query-findOne

findOneAndRemove

http://mongoosejs.com/docs/api.html#query_Query-findOneAndRemove

findOneAndUpdate

http://mongoosejs.com/docs/api.html#query_Query-findOneAndUpdate

#gt

http://mongoosejs.com/docs/api.html#query_Query-gt

#gte

http://mongoosejs.com/docs/api.html#query_Query-gte

#hint

http://mongoosejs.com/docs/api.html#query_Query-hint

#in

http://mongoosejs.com/docs/api.html#query_Query-in

#lean

http://mongoosejs.com/docs/api.html#query_Query-lean

#limit

http://mongoosejs.com/docs/api.html#query_Query-limit

#lt

http://mongoosejs.com/docs/api.html#query_Query-lt

#lte

http://mongoosejs.com/docs/api.html#query_Query-lte

#maxScan

http://mongoosejs.com/docs/api.html#query_Query-maxScan

#merge

http://mongoosejs.com/docs/api.html#query_Query-merge

#mod

http://mongoosejs.com/docs/api.html#query_Query-mod

#ne

http://mongoosejs.com/docs/api.html#query_Query-ne

#nin

http://mongoosejs.com/docs/api.html#query_Query-nin

#nor

http://mongoosejs.com/docs/api.html#query_Query-nor

#or

http://mongoosejs.com/docs/api.html#query_Query-or

#populate

http://mongoosejs.com/docs/api.html#query_Query-populate

#read

http://mongoosejs.com/docs/api.html#query_Query-read

#regex

http://mongoosejs.com/docs/api.html#query_Query-regex

#remove

http://mongoosejs.com/docs/api.html#query_Query-remove

#select

http://mongoosejs.com/docs/api.html#query_Query-select

#setOptions

http://mongoosejs.com/docs/api.html#query_Query-setOptions

#size

http://mongoosejs.com/docs/api.html#query_Query-size

#skip

http://mongoosejs.com/docs/api.html#query_Query-skip

#slice

http://mongoosejs.com/docs/api.html#query_Query-slice

#snapshot

http://mongoosejs.com/docs/api.html#query_Query-snapshot

#sort

http://mongoosejs.com/docs/api.html#query_Query-sort

#stream

http://mongoosejs.com/docs/api.html#query_Query-stream

#tailable

http://mongoosejs.com/docs/api.html#query_Query-tailable

#toConstructor

http://mongoosejs.com/docs/api.html#query_Query-toConstructor

#update

http://mongoosejs.com/docs/api.html#query_Query-update

#where

http://mongoosejs.com/docs/api.html#query_Query-where

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment