0

This is the first model. It's located in the folder called models/user.js

'use strict'

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

    var UserSchema = Schema({

        publications: [{
            description: String,
            categories: [{
                type: Schema.Types.ObjectId,
                ref: 'Category.subcategories'
            }]
    }]

The model category. It's located in the folder called models/category.js

'use strict'

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

    var CategorySchema = Schema({
        name: String,
        subcategories: [{
            name: String
        }]
    });

Look that The UserSchema has categories. This one has this reference:

ref: 'Category.subcategories'

I have a folder called controller. The next json is from controller/category.js

'use strict'

var Categories = require('./models/category');


    function getCategories(req, res){

        Categories.find({},(err, categories) =>{
            if(err){
                res.status(500).send({ message: 'Error en la peticion' });
                return;   
            }

            if(!categories){
                res.status(404).send({ message: 'No hay categories' });
                return
            }

            res.status(200).send({ categories });
        });
    }


    module.exports = {
        getCategories
    }

The json of Category looks like this.

{
    "categories": [
        {
            "subcategories": [
                {
                    "_id": "5ae4a8b0a7510e3bd80917db",
                    "name": "subcategory1"
                },
                {
                    "_id": "5ae4a8b0a7510e3bd80917da",
                    "name": "subcategory2"
                },
                {
                    "_id": "5ae4a8b0a7510e3bd80917d9",
                    "name": "subcategory3"
                }
            ],
            "_id": "5ae4a8b0a7510e3bd80917d7",
            "name": "Category1",
            "__v": 0
        },
        {
            "subcategories": [
                {
                    "_id": "5ae4a8b0a7510e3bd80917e0",
                    "name": "subcategory1"
                },
                {
                    "_id": "5ae4a8b0a7510e3bd80917df",
                    "name": "subcategory2"
                }
            ],
            "_id": "5ae4a8b0a7510e3bd80917dc",
            "name": "Category2",
            "__v": 0
        }
    ]
}

Each subcategory2 belongs to one Category.

This one is in controller/user.js

The user whithout populate will looks like this

        User.find({}, (err, publications) => {
                if (err) {
                    res.status(500).send({
                        message: "Error en la peticion " + err
                    });
                    return;
                }

                if (!publications) {
                    res.status(404).send({
                        message: "Publicacion no encontrada"
                    });
                    return;
                }

res.status(200).send(publications);

result

"publications": [
            {
                "description": "abc",
                "categories": [
                    "5ae4a8b0a7510e3bd80917db",
                    "5ae4a8b0a7510e3bd80917da"
                ]

            },
            {
                "description": "abcddvas asa",
                "categories": [
                    "5ae4a8b0a7510e3bd80917e0"
                ]                
            },
]

I need to populate categories when I do this.

User.find().populate('publications.categories').then(function (err, posa) {
            if (err) {
                res.status(500).send(err);
                return;
            }
            res.status(500).send(posa);
            return;
        });

So the final json should look like this:

"publications": [
                {
                    "description": "abc",
                    "categories": [
                        {
                          "_id": "5ae4a8b0a7510e3bd80917db",
                          "name": "subcategory1"
                        },
                        {
                           "_id": "5ae4a8b0a7510e3bd80917da",
                           "name": "subcategory2"
                        }
                    ]

                },
                {
                    "description": "abcddvas asa",
                    "categories": [
                        {
                           "_id": "5ae4a8b0a7510e3bd80917e0",
                           "name": "subcategory1"
                        }
                    ]                
                },
    ]

But The result does not show me the final json.

I see that there is a option called Dynamic References. http://mongoosejs.com/docs/populate.html

The example show this

var userSchema = new Schema({
  name: String,
  connections: [{
    kind: String,
    item: { type: ObjectId, refPath: 'connections.kind' }
  }]
});

Instead of use ref, uses refPath. But for me didn't work that.

I tried to use this

https://github.com/buunguyen/mongoose-deep-populate

but that didn't work for me.

I tried to do a lot of tries. None works for me. I have been searching days and trying a lot of things but I have not been able to resolve this.

santiago
  • 33
  • 1
  • 8
  • How to you compile your schema? where is your model function and how do you include your sub-documents [your arrays of objects] into your document? – rags2riches-prog May 02 '18 at 13:56
  • I put more info. Is it clearer? – santiago May 02 '18 at 14:22
  • 1) I compile the schema Using mongoose. 2) The model function is in Controller/user.js. 3) I include the array of objectsId into the document. I reply all the answer. Is it clearer?. I am desperate trying to give solution to this. Please let me know if you need more info – santiago May 02 '18 at 14:30
  • You know what? I posted almost a tutorial about the proyect and how to run with the link to download the proyect. I hope tha this is so much clear – santiago May 02 '18 at 16:24
  • https://stackoverflow.com/questions/50139312/populate-nested-array-of-nested-ref – santiago May 02 '18 at 16:24
  • So know you know how it works. how it run. With the link that I posted You will know everything. So what you say now? do you need more details? – santiago May 02 '18 at 16:49
  • I do not even know where to start to help you with this. It is not that the more info you post the better. Quite the contrary, often posting the specific error that you get will be more efficient. So rather than saying "nothing works", be specific about the error that you get...Since you are trying to build a MVC application, start with small things and test your app for each piece of code you write. – rags2riches-prog May 02 '18 at 19:29
  • The app as it stands is an utter mess (sorry to be brutally blunt). The app.js, which is the entry point for the app, misses a lot of middleware functions and the basic functionality for an HTTP server. The controllers contain hard coded data that should be in the model. The models contain schemas with sub-documents that are not correctly implemented. I could go on. – rags2riches-prog May 02 '18 at 19:33
  • Please sort your app out first and then when you run into some specific errors or issues, come back for some help. – rags2riches-prog May 02 '18 at 19:34
  • I do not understand why you asked 'how to compile the schema' and when you say 'array of subdocument objectid' the code shows that. I felt that I lost my time trying to explain you just a specific thing. – santiago May 02 '18 at 19:46
  • I only wanted just populate a nested array of nested model. Like for example ref: model.property. And with that I wanted just populated that. So you said that you wanted all many things and changes everything. – santiago May 02 '18 at 19:46
  • Nothing about you said help me. I spent a lot of time. In this one https://stackoverflow.com/questions/49754557/how-to-update-an-embedded-document-into-a-nested-array the person just put the schema and none said 'how do you compile your schema. show me everything... you know what you are wrong I won't help you because you have bad things. ' – santiago May 02 '18 at 19:46
  • 1
    can you identify the differences between your question and that [question?](https://stackoverflow.com/questions/49754557/how-to-update-an-embedded-document-into-a-nested-array). I am sorry if my questions did not help you this time – rags2riches-prog May 02 '18 at 20:44
  • what about this https://stackoverflow.com/questions/28179720/mongoose-populate-nested-array – santiago May 02 '18 at 21:47
  • the people did not say 'where is your model function' and 'how do you compile ...' – santiago May 02 '18 at 21:48

0 Answers0