Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialdavidlichtsteiner
7,314 PointsOrder of operations
Does it matter if i use .limit(2).skip(sort).sort({title: 1}) or sort({title: 1}).limit(2).skip(2)?
2 Answers
David Kanwisher
9,751 PointsI ran the queries in several different orders and it did not appear to make a difference
> db.posts.find({}, {title: true}).sort({title: -1}) /* show all in collection */
{ "_id" : ObjectId("58f1b04e312bac794e553c60"), "title" : "This is the updated title!" }
{ "_id" : ObjectId("58f1b04e312bac794e553c63"), "title" : "Parenting 101" }
{ "_id" : ObjectId("58f1b04e312bac794e553c61"), "title" : "I love the holidays" }
{ "_id" : ObjectId("58f1b04e312bac794e553c62"), "title" : "How to workout" }
> db.posts.find({}, {title: true}).sort({title: -1}).limit(2).skip(2)
{ "_id" : ObjectId("58f1b04e312bac794e553c61"), "title" : "I love the holidays" }
{ "_id" : ObjectId("58f1b04e312bac794e553c62"), "title" : "How to workout" }
> db.posts.find({}, {title: true}).sort({title: -1}).skip(2).limit(2)
{ "_id" : ObjectId("58f1b04e312bac794e553c61"), "title" : "I love the holidays" }
{ "_id" : ObjectId("58f1b04e312bac794e553c62"), "title" : "How to workout" }
> db.posts.find({}, {title: true}).skip(2).limit(2).sort({title: -1})
{ "_id" : ObjectId("58f1b04e312bac794e553c61"), "title" : "I love the holidays" }
{ "_id" : ObjectId("58f1b04e312bac794e553c62"), "title" : "How to workout" }
> db.posts.find({}, {title: true}).limit(2).skip(2).sort({title: -1})
{ "_id" : ObjectId("58f1b04e312bac794e553c61"), "title" : "I love the holidays" }
{ "_id" : ObjectId("58f1b04e312bac794e553c62"), "title" : "How to workout" }
> db.posts.find({}, {title: true}).limit(2).sort({title: -1}).skip(2)
{ "_id" : ObjectId("58f1b04e312bac794e553c61"), "title" : "I love the holidays" }
{ "_id" : ObjectId("58f1b04e312bac794e553c62"), "title" : "How to workout" }
Daniel Phillips
26,940 PointsIf you're asking about the order of the sort()
, limit()
and skip()
methods, then It matters.
Consider these records (simplified - just titles) : { title: 'C' }, { title: 'B' }, { title: 'D' }, { title: 'A' }
If we want to get this for page 1: { title: 'A' }, { title: 'B' }
and this for page 2: { title: 'C' }, { title: 'D' }
then for the first page we need to do .sort({title: 1}).limit(2).skip(0)
. This will sort our full list of records, then take the first 2.
For the second page, we would use .sort({title: 1}).limit(2).skip(2)
. This finds our records, sorts them, then gives us 2 after skipping the first 2 (so take index 2 and 3 of the sorted records).
If instead for the first page we do: .limit(2).skip(0).sort({title: 1})
then what we are asking for is "find my records, but just 2, don't skip any, then sort them by title". We would get back { title: 'B' }, { title: 'C' }
. This isn't what we expected
Then for the second page if we do: .limit(2).skip(2).sort({title: 1})
then what we are asking for is "find my records, but just 2, and skip the first 2, then sort them by title". We would get back { title: 'A' }, { title: 'D' }
This also incorrect.