Mongoose deep populating multiple 2nd level objects not working

CENT1PEDE

I have a model A wit this field :

var field = {
    foo: String,
    b: [{ type: Schema.Types.ObjectId, ref: 'B' }]
}  

and model B with this feilds :

var field = {
    c: { type: Schema.Types.ObjectId, ref: 'C' } // let's say this has 3 string field
    d: { type: Schema.Types.ObjectId, ref: 'D' } // so was this
}

Based on this answer by Trinh Hoang Nhu by doing this-

 A.find({_id:req.params.id})
    .populate({ path: 'patient', model: Patient,
        populate: {
            path: 'b',
            model: B
        },
        populate: {
            path: 'c',
            model: C
        },
    })
    .exec(function (err, doc) {
        res.send(doc);
    })

-it should return the populated 2nd level and it did but the problem is it only populate the last path declared on the .populate({}) function, in this case only the model C is populated. But when you changed it's position with model B then model B will be populated.

The query above returns something like this :

[
    {
        "foo":"Bar",
        "b": [
            {
                "c":"a32s1da4fas1a23s1da56s4c",
                "d":{
                    "foo1":"Bar1",
                    "foo2":"Bar2",
                    "foo3":"Bar3"
                }
            },
            {
                "c":"a32s1da4fas1a23s1da56s4d",
                "d":{
                    "foo1":"Bar1",
                    "foo2":"Bar2",
                    "foo3":"Bar3"
                }
            }
            // so on ...
        ]
    }
]

I was expecting something like this :

[
    {
        "foo":"Bar",
        "b": [
            {
                "c":{
                    "foo1":"Bar1",
                    "foo2":"Bar2",
                    "foo3":"Bar3"
                },
                "d":{
                    "foo1":"Bar1",
                    "foo2":"Bar2",
                    "foo3":"Bar3"
                }
            },
            {
                "c":{
                    "foo1":"Bar1",
                    "foo2":"Bar2",
                    "foo3":"Bar3"
                },
                "d":{
                    "foo1":"Bar1",
                    "foo2":"Bar2",
                    "foo3":"Bar3"
                }
            }
            // so on ...
        ]
    }
]
Naeem Shaikh

I dont know why it even accepted multiple populate keys in the object, it is already duplicated, what you specified to populate is:

 populate: {
            path: 'b',
            model: B
        },
        populate: {
            path: 'c',
            model: C
        },

here the property populate gets duplicated, and only the last one defined is considered.

You need to specify the populate paths once as an array. So your populate property would become:

 populate: [{
            path: 'b',
            model: B
        },{
            path: 'c',
            model: C
        }]

And the query is,

A.find({_id:req.params.id})
    .populate({ path: 'patient', model: Patient,
        populate: [{
            path: 'b',
            model: B
        },{
            path: 'c',
            model: C
        }],
    })
    .exec(function (err, doc) {
        res.send(doc);
    })

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Mongoose: How to populate 2 level deep population without populating fields of first level? in mongodb

From Dev

Mongoose: How to populate 2 level deep population without populating fields of first level? in mongodb

From Dev

Are objects stored in Hibernates 2nd level cache guaranteed to be "immutable"?

From Dev

2nd selector hover not working in multi level depth menus

From Dev

2nd level callback in android is not working for nested queries

From Dev

Mongoose populating path with multiple subpaths

From Dev

Move all files from multiple level sub folders to 2nd level subfolder

From Dev

How to project filtered 2nd level child objects in Linq to Entities?

From Dev

How does Hibernate 1st and 2nd Level cache work with multiple sessions

From Dev

Spring, and Hibernate 2nd level with Ehcache

From Dev

Explaining 2nd Level Cache

From Dev

OnPropertyChanged 2nd level update

From Dev

How to change 2nd level index into 2nd level column in pandas DataFrame?

From Dev

How to change 2nd level index into 2nd level column in pandas DataFrame?

From Dev

2nd layout Button is not working

From Dev

Python - 2nd 'for' loop in function not working

From Dev

2nd JQuery Code not working

From Dev

Reindex 2nd level in multi-level dataframe

From Dev

VueJS Multiple level objects $SET

From Dev

Ignore 2nd level child using automapper

From Dev

Hibernate inheritance and 2nd level cache proxies

From Dev

EhCache Hibernate 2nd level cache maxBytesLocalHeap slow

From Dev

How to retrieve user 2nd level resource with nested resources

From Dev

Infinispan JPA 2nd level cache defaults

From Dev

Use a CTE to traverse to 2nd level in tree

From Dev

CakePHP - paginate and sort 2nd level association

From Dev

How to access an element from 2nd level parent?

From Dev

Why doesn't this recursive search go to the 2nd level?

From Dev

BreezeJS Predicates on 2nd level expanded entities

Related Related

  1. 1

    Mongoose: How to populate 2 level deep population without populating fields of first level? in mongodb

  2. 2

    Mongoose: How to populate 2 level deep population without populating fields of first level? in mongodb

  3. 3

    Are objects stored in Hibernates 2nd level cache guaranteed to be "immutable"?

  4. 4

    2nd selector hover not working in multi level depth menus

  5. 5

    2nd level callback in android is not working for nested queries

  6. 6

    Mongoose populating path with multiple subpaths

  7. 7

    Move all files from multiple level sub folders to 2nd level subfolder

  8. 8

    How to project filtered 2nd level child objects in Linq to Entities?

  9. 9

    How does Hibernate 1st and 2nd Level cache work with multiple sessions

  10. 10

    Spring, and Hibernate 2nd level with Ehcache

  11. 11

    Explaining 2nd Level Cache

  12. 12

    OnPropertyChanged 2nd level update

  13. 13

    How to change 2nd level index into 2nd level column in pandas DataFrame?

  14. 14

    How to change 2nd level index into 2nd level column in pandas DataFrame?

  15. 15

    2nd layout Button is not working

  16. 16

    Python - 2nd 'for' loop in function not working

  17. 17

    2nd JQuery Code not working

  18. 18

    Reindex 2nd level in multi-level dataframe

  19. 19

    VueJS Multiple level objects $SET

  20. 20

    Ignore 2nd level child using automapper

  21. 21

    Hibernate inheritance and 2nd level cache proxies

  22. 22

    EhCache Hibernate 2nd level cache maxBytesLocalHeap slow

  23. 23

    How to retrieve user 2nd level resource with nested resources

  24. 24

    Infinispan JPA 2nd level cache defaults

  25. 25

    Use a CTE to traverse to 2nd level in tree

  26. 26

    CakePHP - paginate and sort 2nd level association

  27. 27

    How to access an element from 2nd level parent?

  28. 28

    Why doesn't this recursive search go to the 2nd level?

  29. 29

    BreezeJS Predicates on 2nd level expanded entities

HotTag

Archive