Fork me on GitHub


SchemaTypes handle definition of path defaults, validation, getters, setters, field selection defaults for queries and other general characteristics for Strings and Numbers. Check out their respective API documentation for more detail.

Following are all valid Schema Types.


var schema = new Schema({
  name:    String,
  binary:  Buffer,
  living:  Boolean,
  updated: { type: Date, default: },
  age:     { type: Number, min: 18, max: 65 },
  mixed:   Schema.Types.Mixed,
  _someId: Schema.Types.ObjectId,
  array:      [],
  ofString:   [String],
  ofNumber:   [Number],
  ofDates:    [Date],
  ofBuffer:   [Buffer],
  ofBoolean:  [Boolean],
  ofMixed:    [Schema.Types.Mixed],
  ofObjectId: [Schema.Types.ObjectId],
  nested: {
    stuff: { type: String, lowercase: true, trim: true }

// example use

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

var m = new Thing; = 'Statue of Liberty';
m.age = 125;
m.updated = new Date;
m.binary = new Buffer(0); = false;
m.mixed = { any: { thing: 'i want' } };
m._someId = new mongoose.Types.ObjectId;
m.ofDates.addToSet(new Date);
m.ofMixed = [1, [], 'three', { four: 5 }];
m.nested.stuff = 'good';;

SchemaType Options

You can declare a schema type using the type directly, or an object with a type property.

var schema1 = new Schema({
  test: String // `test` is a path of type String

var schema2 = new Schema({
  test: { type: String } // `test` is a path of type string

In addition to the type property, you can specify additional properties for a path. For example, if you want to lowercase a string before saving:

var schema2 = new Schema({
  test: {
    type: String,
    lowercase: true // Always convert `test` to lowercase

The lowercase property only works for strings. There are certain options which apply for all schema types, and some that apply for specific schema types.

All Schema Types
var numberSchema = new Schema({
  integerOnly: {
    type: Number,
    get: v => Math.round(v),
    set: v => Math.round(v)

var Number = mongoose.model('Number', numberSchema);

var doc = new Number();
doc.integerOnly = 2.001;
doc.integerOnly; // 2

can also define [MongoDB indexes]( type options.* `index`: boolean, whether to define an on this property. * `unique`: boolean, whether to define a [unique index]( on this property. * `sparse`: boolean, whether to define a [sparse index]( on this property.

var schema2 = new Schema({
  test: {
    type: String,
    index: true,
    unique: true // Unique index. If you specify `unique: true`
    // specifying `index: true` is optional if you do `unique: true`
Date * `min`: Date * `max`: Date

Usage notes:


Built-in Date methods are not hooked into the mongoose change tracking logic which in English means that if you use a Date in your document and modify it with a method like setMonth(), mongoose will be unaware of this change and will not persist this modification. If you must modify Date types using built-in methods, tell mongoose about the change with doc.markModified('pathToYourDate') before saving.

var Assignment = mongoose.model('Assignment', { dueDate: Date });
Assignment.findOne(function (err, doc) {
  doc.dueDate.setMonth(3);; // THIS DOES NOT SAVE YOUR CHANGE
  doc.markModified('dueDate');; // works


An "anything goes" SchemaType, its flexibility comes at a trade-off of it being harder to maintain. Mixed is available either through Schema.Types.Mixed or by passing an empty object literal. The following are equivalent:

var Any = new Schema({ any: {} });
var Any = new Schema({ any: Object });
var Any = new Schema({ any: Schema.Types.Mixed });

Since it is a schema-less type, you can change the value to anything else you like, but Mongoose loses the ability to auto detect and save those changes. To "tell" Mongoose that the value of a Mixed type has changed, call the .markModified(path) method of the document passing the path to the Mixed type you just changed.

person.anything = { x: [3, 4, { y: "changed" }] };
person.markModified('anything');; // anything will now get saved


To specify a type of ObjectId, use Schema.Types.ObjectId in your declaration.

var mongoose = require('mongoose');
var ObjectId = mongoose.Schema.Types.ObjectId;
var Car = new Schema({ driver: ObjectId });
// or just Schema.ObjectId for backwards compatibility with v2


Provide creation of arrays of SchemaTypes or Sub-Documents.

var ToySchema = new Schema({ name: String });
var ToyBox = new Schema({
  toys: [ToySchema],
  buffers: [Buffer],
  string:  [String],
  numbers: [Number]
  // ... etc

Note: specifying an empty array is equivalent to Mixed. The following all create arrays of Mixed:

var Empty1 = new Schema({ any: [] });
var Empty2 = new Schema({ any: Array });
var Empty3 = new Schema({ any: [Schema.Types.Mixed] });
var Empty4 = new Schema({ any: [{}] });

Creating Custom Types

Mongoose can also be extended with custom SchemaTypes. Search the plugins site for compatible types like mongoose-longmongoose-int32 and other types. To create your own custom schema take a look at Creating a Basic Custom Schema Type.

Next Up

Now that we've covered SchemaTypes, let's take a look at Models.