Skip to content

Instantly share code, notes, and snippets.

@MoSaeid
Last active September 30, 2022 17:36
Show Gist options
  • Save MoSaeid/2a503073d8eecc09d5d2a22e9a703b42 to your computer and use it in GitHub Desktop.
Save MoSaeid/2a503073d8eecc09d5d2a22e9a703b42 to your computer and use it in GitHub Desktop.
Mongoose 6 cheat sheet 2022

npm install mongoose

const mongoose = require('mongoose');

migrating to 6.x

https://mongoosejs.com/docs/migrating_to_6.html

Connect to database

If connecting fails on your machine, try using 127.0.0.1 instead of localhost

mongoose.connect('mongodb://localhost:27017/test123').then(() => { console.log('Connected! successfully'); }) .catch((e) => { console.log("Something went wrong: ", e); })

No More Deprecation Warning Options

useNewUrlParser, useUnifiedTopology, useFindAndModify, and useCreateIndex are no longer supported options. Mongoose 6 always behaves as if useNewUrlParser, useUnifiedTopology, and useCreateIndex are true, and useFindAndModify is false. Please remove these options from your code.

mongoose schema and model export

const catSchema = mongoose.Schema({ name: String, color: String, sex: String })

When you call mongoose.model() on a schema, Mongoose compiles a model for you.

module.exports = mongoose.model('Cat', catSchema);

The first argument is the singular name of the collection your model is for. Mongoose automatically looks for the plural, lowercased version of your model name. Thus, for the example above, the model Cat is for the cats collection in the database.

Note: The .model() function makes a copy of schema.Make sure that you've added everything you want to schema, including hooks, before calling .model()!

mongoose SchemaTypes

const schema = new Schema({
  name:    String,
  binary:  Buffer,
  living:  Boolean,
  updated: { type: Date, default: Date.now },
  age:     { type: Number, min: 18, max: 65 },
  mixed:   Schema.Types.Mixed,
  _someId: Schema.Types.ObjectId,
  decimal: Schema.Types.Decimal128,
  array: [],
  ofString: [String],
  ofNumber: [Number],
  ofDates: [Date],
  ofBuffer: [Buffer],
  ofBoolean: [Boolean],
  ofMixed: [Schema.Types.Mixed],
  ofObjectId: [Schema.Types.ObjectId],
  ofArrays: [[]],
  ofArrayOfNumbers: [[Number]],
  nested: {
    stuff: { type: String, lowercase: true, trim: true }
  },
  map: Map,
  mapOfString: {
    type: Map,
    of: String
  }
})

// example use

const Thing = mongoose.model('Thing', schema);

const m = new Thing;
m.name = 'Statue of Liberty';
m.age = 125;
m.updated = new Date;
m.binary = Buffer.alloc(0);
m.living = false;
m.mixed = { any: { thing: 'i want' } };
m.markModified('mixed');
m._someId = new mongoose.Types.ObjectId;
m.array.push(1);
m.ofString.push("strings!");
m.ofNumber.unshift(1,2,3,4);
m.ofDates.addToSet(new Date);
m.ofBuffer.pop();
m.ofMixed = [1, [], 'three', { four: 5 }];
m.nested.stuff = 'good';
m.map = new Map([['key', 'value']]);
m.save(callback);

to be continued and updated ASAP

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