This is in addition to D. Lowe's answer which worked for me but needed a bit of tweaking. (I would have put this as a comment but I don't have the reputation to do so, and I would like to see this information in a few months when I encounter this issue again but I forget how to solve it.)
If you are importing a Schema from another file, then you will need to add .schema to the end of the import.
Note: I am unsure if you get the Invalid schema configuration if you are not importing schemas and using local schemas instead but importing is just cleaner and easier to handle for me.
For example:
// ./models/other.js
const mongoose = require('mongoose')
const otherSchema = new mongoose.Schema({
content:String,
})
module.exports = mongoose.model('Other', otherSchema)
//*******************SEPERATE FILES*************************//
// ./models/master.js
const mongoose = require('mongoose')
//You will get the error "Invalid schema configuration: `model` is not a valid type" if you omit .schema at the end of the import
const Other=require('./other').schema
const masterSchema = new mongoose.Schema({
others:[Other],
singleOther:Other,
otherInObjectArray:[{
count:Number,
other:Other,
}],
})
module.exports = mongoose.model('Master', masterSchema);
Then, wherever you use this (for me I used code similar to this in my Node.js API) you can simply assign other to master.
For example:
const Master= require('../models/master')
const Other=require('../models/other')
router.get('/generate-new-master', async (req, res)=>{
//load all others
const others=await Other.find()
//generate a new master from your others
const master=new Master({
others,
singleOther:others[0],
otherInObjectArray:[
{
count:1,
other:others[1],
},
{
count:5,
other:others[5],
},
],
})
await master.save()
res.json(master)
})