I am trying to create a document that has an array of another sub-document. I need to be able to export the sub-document for use elsewhere. I am using mongoose.model
and this works, however, it creates an empty collection in my database that is just never used. How can I prevent this behavior?
Usage example:
const mongoose = require("mongoose");
const ChildSchema = new mongoose.Schema({
childName: String
});
const ParentSchema = new mongoose.Schema({
parentName: String,
children: [ChildSchema]
});
module.exports = {
Parent: mongoose.model("parent", ParentSchema),
Child: mongoose.model("child", ChildSchema)
}
CodePudding user response:
You can do it with autoCreate
and autoIndex
options on your Schema:
const ChildSchema = new mongoose.Schema({
autoCreate: false,
autoIndex: false,
childName: String
});
const ParentSchema = new mongoose.Schema({
parentName: String,
children: [ChildSchema]
});