Alex Gurskiy
Alex Gurskiy

Reputation: 376

Mongo db skip-limit work before match after unwind

This is my query:

db.getCollection('grades').
aggregate([{ "$match" : { "class_id" : 28, "student_id" : 0 } }, 
{ "$unwind" : "$scores" }, 
{ "$match" : { "scores.type" : "homework" } }, 
{ "$skip" : 3 }, { "$limit" : 3 }, 
{ "$group" : { "_id" : { "id" : "$_id" }, "scores" : { "$push" : "$scores" } } }, 
{ "$project" : { "_id" : "$_id.id", "scores" : 1 } }])

scores - is a nested array of objects. Score object - {type: "someType", score: someScore}. This query returns one document.

The problem: array of scores has 6 objects and 4 of them have type homework.

The result, what I've received: http://prntscr.com/bq217r

The original document: http://prntscr.com/bq23bv

Why skip-limit performed before match operator? How can I fix it?

Upvotes: 1

Views: 1130

Answers (1)

profesor79
profesor79

Reputation: 9473

As per attached screenshot everything looks OK.

we have 4 elements 1 2 3 4, then we are skipping 3, so we get 4 at the end... and 53 is the value :-)

btw your skip/limit is after $match

Upvotes: 1

Related Questions