I have two schemas.
FAQ Schema
const EventFAQSchema = mongoose.Schema({
question: { type: String, req: true },
answer: { type: String, req: true }
});
EventSchema
const EventSchema = mongoose.Schema({
"title": { type: String },
"description": { type: String },
"eventFAQs": [{
type: EventFAQSchema ,
default: EventFAQSchema
}]
})
I am trying to embed an array of objects of type FAQ. But I get the following error
Undefined type 'undefined' at array 'eventFAQs'
I know I am missing a fundamental concept of array of objects. But I have spent a good deal of time trying to find out the reason and couldn't resolve it myself.
Subdocuments are documents embedded in other documents. In Mongoose, this means you can nest schemas in other schemas. Mongoose has two distinct notions of subdocuments: arrays of subdocuments and single nested subdocuments.
Posted On: Jun 24, 2021. In Mongoose the “_v” field is the versionKey is a property set on each document when first created by Mongoose. This is a document inserted through the mongo shell in a collection and this key-value contains the internal revision of the document.
A SchemaType is different from a type. In other words, mongoose.ObjectId !== mongoose.Types.ObjectId . A SchemaType is just a configuration object for Mongoose. An instance of the mongoose.ObjectId SchemaType doesn't actually create MongoDB ObjectIds, it is just a configuration for a path in a schema.
Subdocuments are documents embedded in other documents. In Mongoose, this means you can nest schemas in other schemas. Mongoose has two distinct notions of subdocuments: arrays of subdocuments and single nested subdocuments.
In Mongoose, subdocuments are documents that are nested in other documents. You can spot a subdocument when a schema is nested in another schema. Note: MongoDB calls subdocuments embedded documents. In practice, you don’t have to create a separate childSchema like the example above.
Mongoose document arrays have a special id method for searching a document array to find a document with a given _id. const doc = parent.children.id (_id); Adding Subdocs to Arrays MongooseArray methods such as push, unshift, addToSet, and others cast arguments to their proper types transparently:
Unlike document arrays, Mongoose 5 does not convert an objects in schemas into nested schemas. In the below example, nested is a nested path rather than a subdocument.
Try with:
"eventFAQs": {
type: [ EventFAQSchema ],
default: [{
question: "default question",
answer: "default answer"
}]
}
EDIT
model.js
const mongoose = require('mongoose');
const EventFAQSchema = mongoose.Schema({
question: { type: String, required: true },
answer: { type: String, required: true }
});
const EventSchema = mongoose.Schema({
"title": { type: String },
"description": { type: String },
"eventFAQs": {
type: [ EventFAQSchema ],
default: [{
question: 'Question',
answer: 'Answer'
}]
}
});
module.exports = mongoose.model('Event', EventSchema);
Usage:
const Event = require('./model.js');
var e = new Event({
title: "Title"
});
e.save(function (err) {
console.log(err); // NO ERROR
});
Result:
{
"_id" : ObjectId("58f99d1a193d534e28bfc70f"),
"title" : "Title",
"eventFAQs" : [
{
"question" : "Question",
"answer" : "Answer",
"_id" : ObjectId("58f99d1a193d534e28bfc70e")
}
],
"__v" : NumberInt(0)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With