English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Limit and Skip Methods in MongoDB

In questa sezione, impareremo come utilizzare MongoDB Limit. Se si desidera leggere una quantità specifica di record in MongoDB, è possibile utilizzare il metodo Limit di MongoDB, il metodo limit() accetta un parametro numerico che specifica il numero di record da leggere da MongoDB.

Metodo Limit() di MongoDB

Per leggere una quantità specifica di record in MongoDB, è necessario utilizzarelimit()Il metodo accetta un parametro di tipo numerico, che è il numero di documenti da visualizzare.

Syntax

limit()La sintassi di base del metodo è la seguente -

>db.COLLECTION_NAME.find().limit(NUMBER)

Example

Supponiamo che la raccolta myycol abbia i seguenti dati.

{"_id": "ObjectId("507f191e810c19729de860e1"), "title": "MongoDB Overview"},
{"_id": "ObjectId("507f191e810c19729de860e2"), "title": "NoSQL Overview"},
{"_id": "ObjectId("507f191e810c19729de860e3"), "title": "w3codebox Overview"}

Esempio di ricerca di documento che mostrerà solo due documenti.

>db.mycol.find({},{"title":1,_id:0}).limit(2)
{"title":"MongoDB Overview"}
{"title":"NoSQL Overview"}
>

If not inlimit()If the number parameter is specified in the method, it will display all documents in the collection.

MongoDB Skip() Method

In addition to the limit() method, there is another methodskip()Also accepts numeric type parameters and is used to skip the number of documents.

Syntax

skip()The basic syntax of the method is as follows:

>db.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER)

Example

The following example will only display the second document.

>db.mycol.find({},{"title":1,_id:0}).limit(1).skip(1)
{"title":"NoSQL Overview"}
>

Please note that,skip()The default value in the method is 0.