In these docs, we'll use the terms 'map' and MongooseMap interchangeably. const SubStrSz = new mongoose.Schema({ value : { type : String, enum : ['qwerty', 'asdf'] } }); const MySchema = new mongoose.Schema({ array: [SubStrSz] }); Using that technique you will able to validate values inside of your array. How to validate an array of string with an enum in Mongoose? I have a model with an enum field, and currently documents can have any single value from the enum. The answer is a little tricky. In Mongoose, maps are how you create a nested document with arbitrary keys. And, moreover, you can't just One question you may ask is why we don't use Symbol instead of primitive values for an enum. Strings have enum as one of the validators. Posted by just now. Installing Mongoose adds all its dependencies, including the MongoDB database driver, but it does not install MongoDB itself. Enumerations in Node.js and Mongoose. enum: Array, creates a validator that checks if the value is in the given array. So enum creates a validator and checks if the value is given in an array. Tag: node.js,mongodb,mongoose. Close. I have an array of strings, that can only contain 6 kind of values. Note: In Mongoose Maps, keys must be strings in order to store the document in MongoDB. Mongoose has several inbuilt validators. In this article, I am going to introduce you to Mongoose and MongoDB, and more importantly where these technologies fit in to your application. Mongoose doesn't support dictionaries so I'm forced to use object literals in an array instead, like so: offersInCategory: [{ category: { type: String, enum: ['Furniture', 'Household', 'Electronicts', 'Other'] }, val: { type: Number, min: 0 } }] My problem with this approach is it feels unintuitive. Mongoose is a JavaScript framework that is commonly used in a Node.js application with a MongoDB database. Enumerations in Node.js and Mongoose. enum: This allows us to set the allowed values of a string. How to validate an array of string with an enum in Mongoose? Mongoose is an Object Data Modeling (ODM) tool for MongoDB and Node.js. enum: Array, creates a validator that checks if the value is in the given array. minlength: Mongoose can also be extended with custom SchemaTypes. Read A MongooseMap is a subclass of JavaScript's Map class. Mongoose regulates association between data, provides schema validation. Search the plugins site for compatible types like mongoose-long, mongoose-int32, and other types. Enumerations in Node.js and Mongoose Richard Clayton. It is also possible to trigger it manually by calling the validate() method. I've written this validation in my Mongoose schema: I have a model with an enum field, and currently documents can have any single value from the enum. Some of the Built-in validators include: Strings: minlength, maxlength, match, enum; Numbers: min, max; Dates: min, max Mongoose Brief Introduction. new mongoose.Schema({ name: { type: String, required: true } }) Validation logic is executed by Mongoose before a document can be saved to the database. minlength: Schema.Types.ObjectId, required: true, // Instead of a hardcoded model name in `ref`, `refPath` means Mongoose // will look at the `onModel` property to find the right model. Stumbling my way through the great wastelands of enterprise software development. Mongoose v5.10.5: Validation, You can think of a Mongoose schema as the configuration object for a Mongoose enum : Array, creates a validator that checks if the value is in the given array. I want documents to be allowed to have an array of values, but have mongoose enforce that all of the values are valid options that exist in the enum - is that possible? Is it possible to create a multi-select enum in mongoose. Vote. New in Mongoose 5.1.0. Mongoose helps in making the connection between an object