Dmitry Minkovsky Dmitry Minkovsky - 27 days ago 11
Node.js Question

How do I define a key named "type" in Mongoose?

I have a Schema definition with a nested object that looks like this:

mongoose.Schema({
name: String,
messages: [{
type: String,
message: String
}]
});


Mongoose doesn't interpret this as I would like because there is a key named
type
, which conflicts with Mongoose's syntax for defining defaults, etc. Is there a way to define a key named "type"?

Answer

Oh, I remember this annoying problem, it took me ages to find out that the problem is that type is read by mongoose schema.

Just specify a type:String inside the type label

mongoose.Schema({
  name: String,
  messages: [{
    type: {type: String},
    message: String
  }]
});
Comments