How do I require an array with objects with specific values in Mongoose.js?

In a personal project of mine I use Mongoose.js to store information in a MongoDB Database.

I have a schema like:

const downloadSchema = new mongoose.Schema({
title: {
        type: String,
        unique: false,
        required: true,
        trim: false
    },
author: {
        type: String,
        unique: false,
        required: true,
        trim: true
}
})

I would like to add a file “versions” to the schema, it should be an array with an object like:

{
 version: Number,
 link: String
}

How would I implement this into the schema?

I did a similar thing with the voting app responses: https://github.com/JohnnyBizzel/voting-app-final/blob/no-auth0/models/Polls.js

2 Likes

On my city’s local developer Slack group they recommended using Mongoose “sub-docs” which are working perfectly for me :slight_smile:

1 Like