These are chat archives for Automattic/mongoose

3rd
Jul 2015
Erinç Fırtına
@EricMcRay
Jul 03 2015 13:45
@krkhan User.find({}).where(‘age’).gt(20).populate({ path: ’stories’, options: { limit: 5 }})
Erinç Fırtına
@EricMcRay
Jul 03 2015 15:09
nested models’ validations and indexes not working is this normal?
Kamran Riaz Khan
@krkhan
Jul 03 2015 16:41
@EricMcRay That limits stories for each user to at most 5. I want to get at most 5 stories. For example, if each user has 2 stories and there are 10 users I want first 3 users, not all 10 of them
Erinç Fırtına
@EricMcRay
Jul 03 2015 16:43
@krkhan you can do it with aggaregation
MrAtheist
@MrAtheist
Jul 03 2015 17:49
hi, any gridfs guru here?
Kamran Riaz Khan
@krkhan
Jul 03 2015 18:17

@EricMcRay Thanks! I came up with this, although now I face a different issue.

mongoose.User.aggregate([{$match: {username: {$ne: 'alex'}}},
                         {$project: {_id: 1, stories: 1}},
                         {$unwind: '$foods'},
                         {$skip: 0},
                         {$limit: 5},
                         {$group: {_id: "$_id", stories: {$push: "$stories"}}}])

This gives me the expected "5 stories at most" while grouped on users. I want to add "stories containing word foo", but can't figure out how to mix populate with aggregate.

Sorry, s/foods/stories/g
Kamran Riaz Khan
@krkhan
Jul 03 2015 18:25
I assume I'm doing something horribly wrong while modeling though. I want to do: "give me at most 5 stories that contain the word "foo" from any user not named alex". That is, I have two criteria: one for parent doc one for the child and I want to do skip/limit on child.
Erinç Fırtına
@EricMcRay
Jul 03 2015 18:39
@krkhan if you store user also in story model you can do it easier. if you do it like this first find users who are older then 20 then find stories