MongoDB equivalent of SQL “TOP” [closed]

牧云@^-^@ 提交于 2020-01-03 08:55:10

问题


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.


回答1:


.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)



回答2:


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



来源:https://stackoverflow.com/questions/13310934/mongodb-equivalent-of-sql-top

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!