Search code examples
javascriptmongoose-schema

Create a Mongoose Schema of type Array of Maps


I am trying to create a mongoose schema that is an array of Maps that are String -> String:

var daySchema = new mongoose.Schema({
    schedule:  {
        type: [Map], 
        of: String
    }
});

This is what I have, but it's giving me a validation error.


Solution

  • How about creating a separate schema for the maps and then use that schema in an array:

    var Schedule = new mongoose.Schema({
        type: Map,
        of: String
    });
    
    var daySchema = new mongoose.Schema({
        type: [Schedule]
    });
    

    As shown in the array examples.