Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query on populate field #168

Open
necm1 opened this issue Apr 20, 2022 · 1 comment
Open

Query on populate field #168

necm1 opened this issue Apr 20, 2022 · 1 comment

Comments

@necm1
Copy link

necm1 commented Apr 20, 2022

Hi!

I tried multiple times to query my populate field to get entries of a specific user. Actually, I don't really know how to do this.

I tried to use the match object but it gives me null as the result including multiple entries not belonging to the user:

    const posts = await this.post.paginate(undefined, {
      limit: 10,
      page,
      populate: [
        {
          path: 'user',
          match: {_id: user.id},
          populate: {path: 'profile', model: 'Profile'},
        },
        {path: 'book'},
      ],
      sort: {_id: -1},
    });

That's the result I get (one entry of multiple entries which doesn't match the user id):

  {
    user: null,
    book: {
      created_at: 2021-12-16T05:31:45.802Z,
      image: null,
      authors: [Array],
      isbn: [Array],
      title: 'test book',
      id: '61bacf4112c14ff9d312523c'
    },
    type: 'post',
    content: 'test',
    created_at: 2021-12-02T11:11:19.000Z,
    updated_at: 2021-12-02T11:11:19.000Z,
    id: '625d7402eb84216a256d1df3'
  },

Is there any way to perform a WHERE-Condition in populate to get only entries of a specific user?

@aravindnc
Copy link
Owner

@necm1 You can try https://www.npmjs.com/package/mongoose-aggregate-paginate-v2 which might be good in your case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants