Skip to main content

Schema Options in Mongoose

Schema Options in Mongoose | Rustcode

Schema Options in Mongoose

Mongoose schema options let you configure the behavior of your schemas and documents. These options enable features like automatic timestamping, version control, strictness in data input, output transformations, and more. This article covers the most common and useful schema options to help you customize your Mongoose models effectively.


timestamps

Set timestamps: true adds createdAt and updatedAt fields automatically to your schema and keeps them updated.

const schema = new mongoose.Schema({
  name: String
}, { timestamps: true });

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

versionKey

The versionKey option controls the internal document versioning field __v. Disable it by setting to false.

const schema = new mongoose.Schema({
  name: String
}, { versionKey: false });

strict

The strict option enforces that only fields specified in the schema will be saved in the database. By default, it is true. Setting it to false allows extra fields.

const schema = new mongoose.Schema({
  name: String
}, { strict: false });

toJSON and toObject

Customize the output formatting of documents when converting to JSON or plain objects. These options allow including virtuals, transforming output, or hiding fields.

const schema = new mongoose.Schema({
  name: String
}, {
  toJSON: { virtuals: true, transform: (doc, ret) => { delete ret._id; } },
  toObject: { virtuals: true }
});

id Option

Mongoose adds a virtual id getter by default (returns _id.toString()). Disable it by setting id: false.

const schema = new mongoose.Schema({
  name: String
}, { id: false });

Other Common Options

  • minimize: Removes empty objects from documents before saving (default true).
  • autoIndex: Automatically builds indexes defined in the schema (default true in development).
  • bufferCommands: Buffers commands if not connected to MongoDB yet (default true).
  • discriminatorKey: Sets key for schema inheritance with discriminators.

Quick Reference Table

Option Purpose Example
timestamps Auto adds and manages createdAt and updatedAt timestamps { timestamps: true }
versionKey Enables or disables the __v version field { versionKey: false }
strict Enforce only defined fields saved to DB { strict: true }
toJSON, toObject Transform output of documents { toJSON: { virtuals: true } }
id Enable or disable virtual id { id: false }

Best Practices

  • Use timestamps for automatic tracking of document creation and updates.
  • Disable versionKey only if you do not need Mongoose document versioning.
  • Keep strict as true to prevent saving unintended fields.
  • Leverage toJSON and toObject transforms for API exposure and serialization.
  • Document schema options clearly in your projects for maintainability.

Conclusion

Schema options in Mongoose provide essential configurability that affects document behavior, validation, serialization, and indexing. Using these options smartly will make your data models more robust, maintainable, and fit for production usage.

Comments