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

aggregate with skip can't work #2078

Closed
mani95lisa opened this issue May 16, 2014 · 6 comments
Closed

aggregate with skip can't work #2078

mani95lisa opened this issue May 16, 2014 · 6 comments
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary

Comments

@mani95lisa
Copy link

Hi, guys, please help me out, thank you very much!

Schema

ReportSchema = new Schema
  user:type:Schema.ObjectId,ref:'User',index:sparse:true
  report:[
    qid:type:Schema.ObjectId,ref:'Question'
    value:Schema.Types.Mixed
    remark:String
  ]
  created_at:Date

Aggregate when skip=0 works fine, but when skip!=0, I got nothing. I'm sure there're pages data.

Report.aggregate()
    .unwind('report')
    .match('report.qid':new ObjectId(data.qid))
    .project(
      value:'$report.value'
      remark:'$report.remark'
      user:'$user'
    )
    .sort('created_at':-1)
    .limit(limit)
    .skip(skip)
    .exec (err, result)->
@mani95lisa
Copy link
Author

I resolved this to make skip before limit, is it a bug?

.skip(skip)
.limit(limit)

@Ppaww-Arms
Copy link

It's because if you limit first you can't skip after, if skip > limit you end up out of the limit results so no data.

@vkarpov15 vkarpov15 added the help label May 16, 2014
@vkarpov15
Copy link
Collaborator

@mypawdesign is right. In aggregate, $limit limits the number of documents sent to the next aggregation state, and $skip skips the first N documents, so if $skip is after $limit and $skip >= $limit, you won't get any results. In short, this is expected behavior in MongoDB.

@PARAGJYOTI
Copy link

@mani95lisa Thanks for the trick . and @vkarpov15 , you are right . Seems like expected behavior in mongodb .

@tanmynguyen
Copy link

Hi @mani95lisa
This is how to fix:
db.collection.aggregate([{$match}, {$sort: {}}, {$skip: _skip}, {$limit: _limit}, {$project: {}}])
Hope it's useful for you!

@webexpert4rv
Copy link

@mani95lisa thanks dear, it's working now

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary
Projects
None yet
Development

No branches or pull requests

6 participants