MongoDB equivalent of SQL "TOP" [closed]
Asked Answered
R

2

11

Select Top 10 a, b from users
equivalent sql query in MONGODB

As a part of jqgrid functionality to save the json data and retrieve json data from the DB, I needed this simple SQL query in MONGODB.

Rancor answered 9/11, 2012 at 15:29 Comment(1)
please elabore what you wanted to achieve, what you've tried so far.Schmitt
W
23
.limit()

Example:

db.things.find().limit(10)

Documentation: http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%7B%7Blimit%28%29%7D%7D

As per the comment below, technically, you'd want to sort the collection too, so it would be:

db.things.find().sort({someField:1}).limit(10)
Wedged answered 9/11, 2012 at 15:35 Comment(2)
add sort() in the chain and it's the answer ;-)Jitters
well... i omitted .sort() due to him not having an 'ORDER BY' in his original sql query ;-)Wedged
J
2

Short question, short answer: you can achieve this with sort() and limit()

Jitters answered 9/11, 2012 at 15:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.