1

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.

1
  • 3
    Add the error message to your question. Commented Jan 10, 2019 at 5:32

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This doesn't appear to work at least with mongoose 6.6. This just creates a schema with the literal keys type and of.
0

You could do this:

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

Comments

0

2023 Solution for Mongoose 6.x.x

var daySchema = new mongoose.Schema({
    schedule:  {
        type: [{myKey1:String, myKey2:String}], 
        
    }
});`

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.