Make many populate in deep populate, 2nd levelHow to achieve DBRef on virtualHandle CastError with Mongoose using PromisesWhy is Model.save() not working in Sails.js?Mongoose save() returns before findOne()Is populate implicit and how many levels deep can it go?Node, Mongoose js - Populate never returning a resultMongoose refuses to cast valid string to ObjectIdMongoose deep populate results in [Object] at 2nd levelHandling mongoose population exceptionsMongoose populate sort option

How were these pictures of spacecraft wind tunnel testing taken?

1960s sci-fi novella with a character who is treated as invisible by being ignored

Smart people send dumb people to a new planet on a space craft that crashes into a body of water

Future enhancements for the finite element method

Crossing US border with music files I'm legally allowed to possess

Transform the partial differential equation with new independent variables

Glitch in AC sine wave interfering with phase cut dimming

Comment dit-on « I’ll tell you what » ?

Yandex Programming Contest: Alarms

Could I be denied entry into Ireland due to medical and police situations during a previous UK visit?

What does the behaviour of water on the skin of an aircraft in flight tell us?

What are the benefits of cryosleep?

Is it ok to put a subplot to a story that is never meant to contribute to the development of the main plot?

Why doesn't the Earth's acceleration towards the Moon accumulate to push the Earth off its orbit?

The Passive Wisdom (Perception) score of my character on D&D Beyond seems too high

Were pen cap holes designed to prevent death by suffocation if swallowed?

Leading and Suffering Numbers

How does apt-get work, in detail?

What is the best linguistic term for describing the kw > p / gw > b change, and its usual companion s > h

Crossword gone overboard

Draw a checker pattern with a black X in the center

Why does the UK have more political parties than the US?

How to extract lower and upper bound in numeric format from a confidence interval string?

What caused the tendency for conservatives to not support climate change reform?



Make many populate in deep populate, 2nd level


How to achieve DBRef on virtualHandle CastError with Mongoose using PromisesWhy is Model.save() not working in Sails.js?Mongoose save() returns before findOne()Is populate implicit and how many levels deep can it go?Node, Mongoose js - Populate never returning a resultMongoose refuses to cast valid string to ObjectIdMongoose deep populate results in [Object] at 2nd levelHandling mongoose population exceptionsMongoose populate sort option






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I am doing a query of nested objects using populate and when wanting to access a population of the second level, it does not recognize me if not the last population



I'm working under nodejs and mongodb, using the mongoose library



For the population of 'tours_favoritos' only recognizes me the last: 'guia' with its population of 'idiomas'. I do not populate 'sitios', nor 'turistas', nor 'categoria', nor 'empresa_turismo'. If I change the order of the population, only the last one populates me.



servicev1.addGustoFavorito = function (id_turista, id_gusto) 
return new Promise(function (resolve, reject)
GustoTurista.findById(id_gusto, function (error, gusto)
if (!error)
var query = Turista.findByIdAndUpdate(id_turista, $push: gustos: gusto._id , new: true );
query.
populate(
path: 'empresas_favoritas', model: EmpresaTurismo
).
populate(
path: 'tours_favoritos',
model: Tour,
populate: path: 'sitios', model: Sitio ,
populate: path: 'turistas', model: Turista ,
populate: path: 'categoria', model: CategoriaTour ,
populate: path: 'empresa_turismo', model: EmpresaTurismo ,
populate:
path: 'guia', model: Guia,
populate: path: 'idiomas', model: Idioma

).
populate(
path: 'gustos', model: GustoTurista
)
.exec(function (err, success)
if (err)
return reject(err);

else
return resolve(success);

)
else
return reject(error);

)
)




See code










share|improve this question



















  • 1





    The problem here is that without any knowledge of how the models actually interrelate and what the expected result would be we cannot determine what would be wrong with what you are doing. You would basically need to show sample data from each model that would be expected to be retrieved by all of the populate() calls. The real big problem here though is that's a lot of joins and MongoDB just is not designed to be used like that. Furthermore you probably do not realize that every time you type populate that is effectively another query to the server. I count 11 requests here.

    – Neil Lunn
    Mar 24 at 9:13






  • 1





    Basically 11 requests to the server ( each findBy and every mention on populate ) just to return some data on a page is extremely excessive and will cause major performance problems for you ongoing. You really need to redesign this since you are still thinking far too much in relational terms. $lookup can help by making that one request, but the "joins" are still a significant overhead here.

    – Neil Lunn
    Mar 24 at 9:15


















0















I am doing a query of nested objects using populate and when wanting to access a population of the second level, it does not recognize me if not the last population



I'm working under nodejs and mongodb, using the mongoose library



For the population of 'tours_favoritos' only recognizes me the last: 'guia' with its population of 'idiomas'. I do not populate 'sitios', nor 'turistas', nor 'categoria', nor 'empresa_turismo'. If I change the order of the population, only the last one populates me.



servicev1.addGustoFavorito = function (id_turista, id_gusto) 
return new Promise(function (resolve, reject)
GustoTurista.findById(id_gusto, function (error, gusto)
if (!error)
var query = Turista.findByIdAndUpdate(id_turista, $push: gustos: gusto._id , new: true );
query.
populate(
path: 'empresas_favoritas', model: EmpresaTurismo
).
populate(
path: 'tours_favoritos',
model: Tour,
populate: path: 'sitios', model: Sitio ,
populate: path: 'turistas', model: Turista ,
populate: path: 'categoria', model: CategoriaTour ,
populate: path: 'empresa_turismo', model: EmpresaTurismo ,
populate:
path: 'guia', model: Guia,
populate: path: 'idiomas', model: Idioma

).
populate(
path: 'gustos', model: GustoTurista
)
.exec(function (err, success)
if (err)
return reject(err);

else
return resolve(success);

)
else
return reject(error);

)
)




See code










share|improve this question



















  • 1





    The problem here is that without any knowledge of how the models actually interrelate and what the expected result would be we cannot determine what would be wrong with what you are doing. You would basically need to show sample data from each model that would be expected to be retrieved by all of the populate() calls. The real big problem here though is that's a lot of joins and MongoDB just is not designed to be used like that. Furthermore you probably do not realize that every time you type populate that is effectively another query to the server. I count 11 requests here.

    – Neil Lunn
    Mar 24 at 9:13






  • 1





    Basically 11 requests to the server ( each findBy and every mention on populate ) just to return some data on a page is extremely excessive and will cause major performance problems for you ongoing. You really need to redesign this since you are still thinking far too much in relational terms. $lookup can help by making that one request, but the "joins" are still a significant overhead here.

    – Neil Lunn
    Mar 24 at 9:15














0












0








0








I am doing a query of nested objects using populate and when wanting to access a population of the second level, it does not recognize me if not the last population



I'm working under nodejs and mongodb, using the mongoose library



For the population of 'tours_favoritos' only recognizes me the last: 'guia' with its population of 'idiomas'. I do not populate 'sitios', nor 'turistas', nor 'categoria', nor 'empresa_turismo'. If I change the order of the population, only the last one populates me.



servicev1.addGustoFavorito = function (id_turista, id_gusto) 
return new Promise(function (resolve, reject)
GustoTurista.findById(id_gusto, function (error, gusto)
if (!error)
var query = Turista.findByIdAndUpdate(id_turista, $push: gustos: gusto._id , new: true );
query.
populate(
path: 'empresas_favoritas', model: EmpresaTurismo
).
populate(
path: 'tours_favoritos',
model: Tour,
populate: path: 'sitios', model: Sitio ,
populate: path: 'turistas', model: Turista ,
populate: path: 'categoria', model: CategoriaTour ,
populate: path: 'empresa_turismo', model: EmpresaTurismo ,
populate:
path: 'guia', model: Guia,
populate: path: 'idiomas', model: Idioma

).
populate(
path: 'gustos', model: GustoTurista
)
.exec(function (err, success)
if (err)
return reject(err);

else
return resolve(success);

)
else
return reject(error);

)
)




See code










share|improve this question
















I am doing a query of nested objects using populate and when wanting to access a population of the second level, it does not recognize me if not the last population



I'm working under nodejs and mongodb, using the mongoose library



For the population of 'tours_favoritos' only recognizes me the last: 'guia' with its population of 'idiomas'. I do not populate 'sitios', nor 'turistas', nor 'categoria', nor 'empresa_turismo'. If I change the order of the population, only the last one populates me.



servicev1.addGustoFavorito = function (id_turista, id_gusto) 
return new Promise(function (resolve, reject)
GustoTurista.findById(id_gusto, function (error, gusto)
if (!error)
var query = Turista.findByIdAndUpdate(id_turista, $push: gustos: gusto._id , new: true );
query.
populate(
path: 'empresas_favoritas', model: EmpresaTurismo
).
populate(
path: 'tours_favoritos',
model: Tour,
populate: path: 'sitios', model: Sitio ,
populate: path: 'turistas', model: Turista ,
populate: path: 'categoria', model: CategoriaTour ,
populate: path: 'empresa_turismo', model: EmpresaTurismo ,
populate:
path: 'guia', model: Guia,
populate: path: 'idiomas', model: Idioma

).
populate(
path: 'gustos', model: GustoTurista
)
.exec(function (err, success)
if (err)
return reject(err);

else
return resolve(success);

)
else
return reject(error);

)
)




See code







node.js mongodb mongoose mongoose-schema mongoose-populate






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 24 at 8:51







WI HERMES

















asked Mar 24 at 8:44









WI HERMESWI HERMES

11




11







  • 1





    The problem here is that without any knowledge of how the models actually interrelate and what the expected result would be we cannot determine what would be wrong with what you are doing. You would basically need to show sample data from each model that would be expected to be retrieved by all of the populate() calls. The real big problem here though is that's a lot of joins and MongoDB just is not designed to be used like that. Furthermore you probably do not realize that every time you type populate that is effectively another query to the server. I count 11 requests here.

    – Neil Lunn
    Mar 24 at 9:13






  • 1





    Basically 11 requests to the server ( each findBy and every mention on populate ) just to return some data on a page is extremely excessive and will cause major performance problems for you ongoing. You really need to redesign this since you are still thinking far too much in relational terms. $lookup can help by making that one request, but the "joins" are still a significant overhead here.

    – Neil Lunn
    Mar 24 at 9:15













  • 1





    The problem here is that without any knowledge of how the models actually interrelate and what the expected result would be we cannot determine what would be wrong with what you are doing. You would basically need to show sample data from each model that would be expected to be retrieved by all of the populate() calls. The real big problem here though is that's a lot of joins and MongoDB just is not designed to be used like that. Furthermore you probably do not realize that every time you type populate that is effectively another query to the server. I count 11 requests here.

    – Neil Lunn
    Mar 24 at 9:13






  • 1





    Basically 11 requests to the server ( each findBy and every mention on populate ) just to return some data on a page is extremely excessive and will cause major performance problems for you ongoing. You really need to redesign this since you are still thinking far too much in relational terms. $lookup can help by making that one request, but the "joins" are still a significant overhead here.

    – Neil Lunn
    Mar 24 at 9:15








1




1





The problem here is that without any knowledge of how the models actually interrelate and what the expected result would be we cannot determine what would be wrong with what you are doing. You would basically need to show sample data from each model that would be expected to be retrieved by all of the populate() calls. The real big problem here though is that's a lot of joins and MongoDB just is not designed to be used like that. Furthermore you probably do not realize that every time you type populate that is effectively another query to the server. I count 11 requests here.

– Neil Lunn
Mar 24 at 9:13





The problem here is that without any knowledge of how the models actually interrelate and what the expected result would be we cannot determine what would be wrong with what you are doing. You would basically need to show sample data from each model that would be expected to be retrieved by all of the populate() calls. The real big problem here though is that's a lot of joins and MongoDB just is not designed to be used like that. Furthermore you probably do not realize that every time you type populate that is effectively another query to the server. I count 11 requests here.

– Neil Lunn
Mar 24 at 9:13




1




1





Basically 11 requests to the server ( each findBy and every mention on populate ) just to return some data on a page is extremely excessive and will cause major performance problems for you ongoing. You really need to redesign this since you are still thinking far too much in relational terms. $lookup can help by making that one request, but the "joins" are still a significant overhead here.

– Neil Lunn
Mar 24 at 9:15






Basically 11 requests to the server ( each findBy and every mention on populate ) just to return some data on a page is extremely excessive and will cause major performance problems for you ongoing. You really need to redesign this since you are still thinking far too much in relational terms. $lookup can help by making that one request, but the "joins" are still a significant overhead here.

– Neil Lunn
Mar 24 at 9:15













1 Answer
1






active

oldest

votes


















0














The mistake here is in this second populate query:



.populate(
path: 'tours_favoritos',
model: Tour,
populate: path: 'sitios', model: Sitio ,
populate: path: 'turistas', model: Turista ,
populate: path: 'categoria', model: CategoriaTour ,
populate: path: 'empresa_turismo', model: EmpresaTurismo ,
populate:
path: 'guia', model: Guia,
populate: path: 'idiomas', model: Idioma

)


You are passing an object to populate function, and everytime you are using another populate inside that object, it overrides the old one, it is similar to any object definition.



Eg:



var a=
foo : "bar",
foo : "bar2",
foo : "bar3"


//the last foo overrides the first two
//a.foo = bar3


just like above example, last populate is overriding the previous populates.



Now back to solve your question,



You need to pass array to populate field, so that it populates all the given fields you want



Try this:



.populate(
path: 'tours_favoritos',
model: Tour,
populate: [
path: 'sitios', model: Sitio ,
path: 'turistas', model: Turista ,
path: 'categoria', model: CategoriaTour ,
path: 'empresa_turismo', model: EmpresaTurismo ,

path: 'guia', model: Guia,
populate: path: 'idiomas', model: Idioma

]
)


replace the above code in place of your second populate query and it will work.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55322030%2fmake-many-populate-in-deep-populate-2nd-level%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    The mistake here is in this second populate query:



    .populate(
    path: 'tours_favoritos',
    model: Tour,
    populate: path: 'sitios', model: Sitio ,
    populate: path: 'turistas', model: Turista ,
    populate: path: 'categoria', model: CategoriaTour ,
    populate: path: 'empresa_turismo', model: EmpresaTurismo ,
    populate:
    path: 'guia', model: Guia,
    populate: path: 'idiomas', model: Idioma

    )


    You are passing an object to populate function, and everytime you are using another populate inside that object, it overrides the old one, it is similar to any object definition.



    Eg:



    var a=
    foo : "bar",
    foo : "bar2",
    foo : "bar3"


    //the last foo overrides the first two
    //a.foo = bar3


    just like above example, last populate is overriding the previous populates.



    Now back to solve your question,



    You need to pass array to populate field, so that it populates all the given fields you want



    Try this:



    .populate(
    path: 'tours_favoritos',
    model: Tour,
    populate: [
    path: 'sitios', model: Sitio ,
    path: 'turistas', model: Turista ,
    path: 'categoria', model: CategoriaTour ,
    path: 'empresa_turismo', model: EmpresaTurismo ,

    path: 'guia', model: Guia,
    populate: path: 'idiomas', model: Idioma

    ]
    )


    replace the above code in place of your second populate query and it will work.






    share|improve this answer



























      0














      The mistake here is in this second populate query:



      .populate(
      path: 'tours_favoritos',
      model: Tour,
      populate: path: 'sitios', model: Sitio ,
      populate: path: 'turistas', model: Turista ,
      populate: path: 'categoria', model: CategoriaTour ,
      populate: path: 'empresa_turismo', model: EmpresaTurismo ,
      populate:
      path: 'guia', model: Guia,
      populate: path: 'idiomas', model: Idioma

      )


      You are passing an object to populate function, and everytime you are using another populate inside that object, it overrides the old one, it is similar to any object definition.



      Eg:



      var a=
      foo : "bar",
      foo : "bar2",
      foo : "bar3"


      //the last foo overrides the first two
      //a.foo = bar3


      just like above example, last populate is overriding the previous populates.



      Now back to solve your question,



      You need to pass array to populate field, so that it populates all the given fields you want



      Try this:



      .populate(
      path: 'tours_favoritos',
      model: Tour,
      populate: [
      path: 'sitios', model: Sitio ,
      path: 'turistas', model: Turista ,
      path: 'categoria', model: CategoriaTour ,
      path: 'empresa_turismo', model: EmpresaTurismo ,

      path: 'guia', model: Guia,
      populate: path: 'idiomas', model: Idioma

      ]
      )


      replace the above code in place of your second populate query and it will work.






      share|improve this answer

























        0












        0








        0







        The mistake here is in this second populate query:



        .populate(
        path: 'tours_favoritos',
        model: Tour,
        populate: path: 'sitios', model: Sitio ,
        populate: path: 'turistas', model: Turista ,
        populate: path: 'categoria', model: CategoriaTour ,
        populate: path: 'empresa_turismo', model: EmpresaTurismo ,
        populate:
        path: 'guia', model: Guia,
        populate: path: 'idiomas', model: Idioma

        )


        You are passing an object to populate function, and everytime you are using another populate inside that object, it overrides the old one, it is similar to any object definition.



        Eg:



        var a=
        foo : "bar",
        foo : "bar2",
        foo : "bar3"


        //the last foo overrides the first two
        //a.foo = bar3


        just like above example, last populate is overriding the previous populates.



        Now back to solve your question,



        You need to pass array to populate field, so that it populates all the given fields you want



        Try this:



        .populate(
        path: 'tours_favoritos',
        model: Tour,
        populate: [
        path: 'sitios', model: Sitio ,
        path: 'turistas', model: Turista ,
        path: 'categoria', model: CategoriaTour ,
        path: 'empresa_turismo', model: EmpresaTurismo ,

        path: 'guia', model: Guia,
        populate: path: 'idiomas', model: Idioma

        ]
        )


        replace the above code in place of your second populate query and it will work.






        share|improve this answer













        The mistake here is in this second populate query:



        .populate(
        path: 'tours_favoritos',
        model: Tour,
        populate: path: 'sitios', model: Sitio ,
        populate: path: 'turistas', model: Turista ,
        populate: path: 'categoria', model: CategoriaTour ,
        populate: path: 'empresa_turismo', model: EmpresaTurismo ,
        populate:
        path: 'guia', model: Guia,
        populate: path: 'idiomas', model: Idioma

        )


        You are passing an object to populate function, and everytime you are using another populate inside that object, it overrides the old one, it is similar to any object definition.



        Eg:



        var a=
        foo : "bar",
        foo : "bar2",
        foo : "bar3"


        //the last foo overrides the first two
        //a.foo = bar3


        just like above example, last populate is overriding the previous populates.



        Now back to solve your question,



        You need to pass array to populate field, so that it populates all the given fields you want



        Try this:



        .populate(
        path: 'tours_favoritos',
        model: Tour,
        populate: [
        path: 'sitios', model: Sitio ,
        path: 'turistas', model: Turista ,
        path: 'categoria', model: CategoriaTour ,
        path: 'empresa_turismo', model: EmpresaTurismo ,

        path: 'guia', model: Guia,
        populate: path: 'idiomas', model: Idioma

        ]
        )


        replace the above code in place of your second populate query and it will work.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 27 at 15:41









        Ravi Shankar BhartiRavi Shankar Bharti

        4,34321439




        4,34321439





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55322030%2fmake-many-populate-in-deep-populate-2nd-level%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript