MongoDB相当于SQL"TOP" [英] MongoDB equivalent of SQL "TOP"

查看:223
本文介绍了MongoDB相当于SQL"TOP"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从用户中选择前10名a,b
MONGODB中的等效SQL查询

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

作为用于保存json数据并从数据库中检索json数据的jqgrid功能的一部分,我需要在MONGODB中使用此简单的SQL查询.

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.

推荐答案

.limit()

示例:

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

文档: 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)

这篇关于MongoDB相当于SQL"TOP"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆