Search code examples
javascriptmongodbmongoose-schema

Array as value in mongoose schema using javascript


I am performing CRUD operations using MEAN stack. I am using mongoose and I have created a schema also.

discussions.js

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const discussionSchema = new Schema({
  question: String,
  date: String,
  askedby: String,
  answers: String[],
  tags: String[]
})

module.exports = mongoose.model('discussion', discussionSchema, 'discussions');

Notice that answers and tags properties should be an array of string type while rest of the properties are simple string values. Is this the correct implementation? Please correct me.


Solution

  • The correct way to define is :

      answers :[String],
      tags:[String]
    

    or

    answers :[{ type: String }],
    tags:[{ type: String }]
    

    Reference: https://mongoosejs.com/docs/schematypes.html