zoukankan      html  css  js  c++  java
  • 菜鸟的mongoDB学习---(五)MongoDB的limit、skip、sort方法

    limit方法

    假设你须要在MongoDB中读取指定数量的数据记录。能够使用MongoDB的Limit方法,limit()方法接受一个数字參数,该參数指定从MongoDB中读取的记录条数。

    mongo #进入mongoDB
    <span style="line-height: 16.363636016845703px;">use NewsDB #选择NewsDB</span>
    
    db.new.find().limit(5) #列出前5条文档 

    注:假设你们没有指定limit()方法中的參数则显示集合中的全部数据。


    skip方法

    我们除了能够使用limit()方法来读取指定数量的数据外。还能够使用skip()方法来跳过指定数量的数据,skip方法相同接受一个数字參数作为跳过的记录条数。


    db.new.find().skip(5).limit(5) #跳过前5条文档,列出6-10条文档

    注:skip()方法默认參数为 0 。


    sort方法

    在MongoDB中使用使用sort()方法对数据进行排序,sort()方法能够通过參数指定排序的字段,并使用 1 和 -1 来指定排序的方式,当中 1 为升序排列。而-1是用于降序排列。

    db.new.find({},{"title":1,_id:0}).sort({"title":-1}) #仅仅显示title属性,并将其降序排列显示








  • 相关阅读:
    uva 11294 Wedding
    uvalive 4452 The Ministers’ Major Mess
    uvalive 3211 Now Or Later
    uvalive 3713 Astronauts
    uvalive 4288 Cat Vs. Dog
    uvalive 3276 The Great Wall Game
    uva 1411 Ants
    uva 11383 Golden Tiger Claw
    uva 11419 SAM I AM
    uvalive 3415 Guardian Of Decency
  • 原文地址:https://www.cnblogs.com/liguangsunls/p/6848290.html
Copyright © 2011-2022 走看看