How to populate an object inside an object in an array?

Takumasakix

I am currently working on a project with mongoose, and I have been stuck in a problem with the mongoose populate() method.

The problem is that I cannot populate an object inside an object in an array.

// * I simplified the code below.

// Profile Document
Profile = model('Profile', new Schema({
    _user: {
      type: Schema.Types.ObjectId,
      ref: 'User',
    },
    posts: [
      {
        type: Schema.Types.ObjectId,
        ref: 'Post',
        // Post = { 
        //   _id: Number,
        //   _poster: { 
        //     type: Schema.Types.ObjectId,
        //     ref: 'User',
        //   }
        // };
      },
    ],
}));

// app.js
 const profile = await Profile.findOne({ _user: userId })
    .populate('_user')  // it works
    .populate('posts')  // it works too
    .populate('posts._poster'); // it doesn't work

Is there any way to populate a nested object in an array?

It'd be great if you could answer my question. Thank you in advance.



I tried accessing the notation properly to populate *_poster*, however, it didn't still work. The code is below.

    const profile = await Profile.findOne({ _user: userId })
      .populate('_user')
      .populate('posts');

    await profile.toObject();
    profile.posts.forEach((post) => {
      profile.populate('posts.post._poster');
    })

Indraraj26

I have tested your schema this works great, could you test and let me know

const profile = await Profile.findOne({
        _id: '5f108ed7ecc4881a6c35e27b',
    })
        .populate('_user')
        .populate({
            path: 'posts',
            populate: {
                path: '_poster',
                model: 'User',
            },
        })
        .exec();
    console.log(profile);

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Push array into object inside loop

분류에서Dev

Push array into object inside loop

분류에서Dev

How can I sort this object by 'sortby' key which inside an array?

분류에서Dev

How to get array object in object

분류에서Dev

Rendering array inside object to a table using react

분류에서Dev

Parsing JSON Object inside Array in Jackson

분류에서Dev

How to access attributes of php object inside a array of php objects from javascript

분류에서Dev

how to sum up all values with same keys in an object inside an array and get their average

분류에서Dev

How can I change the string object inside a string object?

분류에서Dev

How to replace an object value in an array?

분류에서Dev

How to call a method from inside an object?

분류에서Dev

How to cast shadow to an object which is inside a sphere

분류에서Dev

How to update document in an object in an array in an object in an array using MongoDB?

분류에서Dev

How do I access an object's keys' values? Looking to populate a table from object data

분류에서Dev

How to return an array of object properties from a returned object of Laravel Eloquent

분류에서Dev

How to compare two object elements in a mongodb array

분류에서Dev

How to get the type of global Array Object function;

분류에서Dev

how to call variable name in array of object in React

분류에서Dev

How to get object from array in swift

분류에서Dev

How can I pass the array object?

분류에서Dev

how to stop rethinkdb converting object to array?

분류에서Dev

How to join the output of object array to a string in PowerShell?

분류에서Dev

how is Array object different from other objects

분류에서Dev

How can I retrieve the object into an array in Javascript?

분류에서Dev

Traversing Through Object + Array + Object

분류에서Dev

Accessing object in array recreating object?

분류에서Dev

Object array to generic array

분류에서Dev

Object[] inside Object[] Java returns memory address?

분류에서Dev

Android Parsing JSON object inside another object

Related 관련 기사

  1. 1

    Push array into object inside loop

  2. 2

    Push array into object inside loop

  3. 3

    How can I sort this object by 'sortby' key which inside an array?

  4. 4

    How to get array object in object

  5. 5

    Rendering array inside object to a table using react

  6. 6

    Parsing JSON Object inside Array in Jackson

  7. 7

    How to access attributes of php object inside a array of php objects from javascript

  8. 8

    how to sum up all values with same keys in an object inside an array and get their average

  9. 9

    How can I change the string object inside a string object?

  10. 10

    How to replace an object value in an array?

  11. 11

    How to call a method from inside an object?

  12. 12

    How to cast shadow to an object which is inside a sphere

  13. 13

    How to update document in an object in an array in an object in an array using MongoDB?

  14. 14

    How do I access an object's keys' values? Looking to populate a table from object data

  15. 15

    How to return an array of object properties from a returned object of Laravel Eloquent

  16. 16

    How to compare two object elements in a mongodb array

  17. 17

    How to get the type of global Array Object function;

  18. 18

    how to call variable name in array of object in React

  19. 19

    How to get object from array in swift

  20. 20

    How can I pass the array object?

  21. 21

    how to stop rethinkdb converting object to array?

  22. 22

    How to join the output of object array to a string in PowerShell?

  23. 23

    how is Array object different from other objects

  24. 24

    How can I retrieve the object into an array in Javascript?

  25. 25

    Traversing Through Object + Array + Object

  26. 26

    Accessing object in array recreating object?

  27. 27

    Object array to generic array

  28. 28

    Object[] inside Object[] Java returns memory address?

  29. 29

    Android Parsing JSON object inside another object

뜨겁다태그

보관