These are chat archives for Automattic/mongoose

4th
Jul 2015
Kamran Riaz Khan
@krkhan
Jul 04 2015 17:12

@EricMcRay That sounds promising! But if I do it like this:

  • Each Story has ref to its parent User
  • Find() stories which contain the word "foo", populate() while providing the required match for User, i.e., age > 20, skip limit etc.

This will give me the expected data albeit it's not grouped by User. Aggregate can only be called on the model directly, how would I group the stories by user after population?

Erinç Fırtına
@EricMcRay
Jul 04 2015 17:52
@krkhan Sorry, I cant answer your question clean. I have lot of work to do right now. So I gonna tell how I would to it so you can try that or maybe that shows you path to find your own solution. This is how I would do:
  • First I get Users who are older than 20 and only select _ids for less data
  • I convert that object array to _ids array with lodash like [{_id: 1},{_id: 2}, {_id: 3}] to [1,2,3]
  • I use that users _id array to filter Stories like user: { $in: UsersIdArray }
    I hope it helps
Kamran Riaz Khan
@krkhan
Jul 04 2015 18:03
@EricMcRay That does help a lot, thanks!!
Kamran Riaz Khan
@krkhan
Jul 04 2015 20:18
Separate question, one of my populates does nothing (returns just the ID) if I don't specify fields to select. If I specify fields it works correctly. E.g., populate('user') doesn't do anything but populate('user', 'username') populates the username correctly. Is that an expected behavior?
Nvm, I just specified all fields anyway, not a big issue right now