SchemaTypes

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.

Example

  1. var schema = new Schema({
  2. name: String,
  3. binary: Buffer,
  4. living: Boolean,
  5. updated: { type: Date, default: Date.now }
  6. age: { type: Number, min: 18, max: 65 }
  7. mixed: Schema.Types.Mixed,
  8. _someId: Schema.Types.ObjectId,
  9. array: [],
  10. ofString: [String],
  11. ofNumber: [Number],
  12. ofDates: [Date],
  13. ofBuffer: [Buffer],
  14. ofBoolean: [Boolean],
  15. ofMixed: [Schema.Types.Mixed],
  16. ofObjectId: [Schema.Types.ObjectId],
  17. nested: {
  18. stuff: { type: String, lowercase: true, trim: true }
  19. }
  20. })
  21. // example use
  22. var Thing = mongoose.model('Thing', schema);
  23. var m = new Thing;
  24. m.name = 'Statue of Liberty'
  25. m.age = 125;
  26. m.updated = new Date;
  27. m.binary = new Buffer(0);
  28. m.living = false;
  29. m.mixed = { any: { thing: 'i want' } };
  30. m.markModified('mixed');
  31. m._someId = new mongoose.Types.ObjectId;
  32. m.array.push(1);
  33. m.ofString.push("strings!");
  34. m.ofNumber.unshift(1,2,3,4);
  35. m.ofDate.addToSet(new Date);
  36. m.ofBuffer.pop();
  37. m.ofMixed = [1, [], 'three', { four: 5 }];
  38. m.nested.stuff = 'good';
  39. m.save(callback);

Usage notes:

Dates

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 doc.save() 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.

  1. var Assignment = mongoose.model('Assignment', { dueDate: Date });
  2. Assignment.findOne(function (err, doc) {
  3. doc.dueDate.setMonth(3);
  4. doc.save(callback) // THIS DOES NOT SAVE YOUR CHANGE
  5. doc.markModified('dueDate');
  6. doc.save(callback) // works
  7. })

Mixed

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:

  1. var Any = new Schema({ any: {} });
  2. 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.

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

ObjectIds

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

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

Arrays

Provide creation of arrays of SchemaTypes or Sub-Documents.

  1. var ToySchema = new Schema({ name: String });
  2. var ToyBox = new Schema({
  3. toys: [ToySchema],
  4. buffers: [Buffer],
  5. string: [String],
  6. numbers: [Number]
  7. // ... etc
  8. });

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

  1. var Empty1 = new Schema({ any: [] });
  2. var Empty2 = new Schema({ any: Array });
  3. var Empty3 = new Schema({ any: [Schema.Types.Mixed] });
  4. 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-long and other types.

Next Up

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