Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array of subdocuments in Mongoose

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.

like image 534
Giridhar Karnik Avatar asked Apr 21 '17 05:04

Giridhar Karnik


People also ask

What are subdocuments in MongoDB?

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.

What is __ V in MongoDB?

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.

What is Mongoose types ObjectId?

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.

What are subdocuments in mongoose?

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.

What are subdocuments in MongoDB?

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.

How do I find a specific document in a mongoose array?

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:

How to convert an object into a nested schema in mongoose 5?

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.


1 Answers

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)
}
like image 170
luisenrike Avatar answered Sep 25 '22 04:09

luisenrike