mongoose Schema max property has no effect

const mongoose = require("mongoose");

const PostSchema = new mongoose.Schema(
  {
    user_id: {
      type: String,
      required: true,
    },
    content: {
      type: String,
      max: 150,
      required: true,
    },
  },
  {
    timestamps: true,
  }
);

As defined above, I can nevertheless push content that has more than 150 characters. What is wrong with how the Query is defined?

Upvotes: 1

Views: 94

Answers (1)

Alexander Staroselsky
Alexander Staroselsky

Reputation: 38777

Type string does not have “max”, instead use “maxLength”:

maxLength: 150,

Documentation about validation that mentions this.

Hopefully that helps!

Upvotes: 1

Related Questions