MongoDB Skip Performance

Have a Database Problem? Speak with an Expert for Free
Get Started >>

Introduction

MongoDB is a NoSQL database, that means, there are no tables. Data is stored in JSON format. A single collection can consist of multiple documents. There can be thousands of documents in a single collection. We don’t always need to go through every document. We can skip the documents using the skip method. In this article, we will learn about the skip method and its performance.

Skip()

The skip method in MongoDB is used to skip the documents while fetching. It is a useful method. Suppose there are tons of documents in a collection. We need to fetch all the documents except the first fifty. How can we do it? We can use the skip method for it. Let’s discuss the skip method in detail.

We will perform skip operation on the details collection.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
> db.details.find().pretty()
{
        "_id" : ObjectId("5d52ff3c2daf944acd3099fb"),
        "name" : "John",
        "location" : "New York"
}
{
        "_id" : ObjectId("5d52ff432daf944acd3099fc"),
        "name" : "Lisa",
        "location" : "New York"
}
{
        "_id" : ObjectId("5d52ff4e2daf944acd3099fd"),
        "name" : "Rosy",
        "location" : "Chicago"
}
{
        "_id" : ObjectId("5d52ff5b2daf944acd3099fe"),
        "name" : "Rocky",
        "location" : "Detroit"
}
{
        "_id" : ObjectId("5d52ff622daf944acd3099ff"),
        "name" : "Sam",
        "location" : "Texas"
}
{
        "_id" : ObjectId("5d52ff6e2daf944acd309a00"),
        "name" : "Bob",
        "location" : "Texas"
}
{
        "_id" : ObjectId("5d52ff802daf944acd309a01"),
        "name" : "Billy",
        "location" : "Washington DC"
}
{
        "_id" : ObjectId("5d52ff932daf944acd309a02"),
        "name" : "Sophie",
        "location" : "New York"
}
{
        "_id" : ObjectId("5d52ffa62daf944acd309a03"),
        "name" : "Leo",
        "location" : "Chicago"
}
{
        "_id" : ObjectId("5d52ffbc2daf944acd309a04"),
        "name" : "Marshal",
        "location" : "California"
}
>

The details collection has ten documents. Each document contains the name of an employee and location. We used the find method to fetch all the documents. Of course, we can use query and projection to filter results but we want to skip the first five documents. We do not need the first five documents. What can we do? Yes, we can use the skip method. Have a look at how we can use the skip method.

1
2
3
4
db.details
  .find()
  .skip(5)
  .pretty();

The skip method is written right after the find method. The value passed to the skip function defines the number of documents we want to skip. We passed five as the value because we want to skip the first five documents. Let’s verify the result of the above command.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
{
        "_id" : ObjectId("5d52ff6e2daf944acd309a00"),
        "name" : "Bob",
        "location" : "Texas"
}
{
        "_id" : ObjectId("5d52ff802daf944acd309a01"),
        "name" : "Billy",
        "location" : "Washington DC"
}
{
        "_id" : ObjectId("5d52ff932daf944acd309a02"),
        "name" : "Sophie",
        "location" : "New York"
}
{
        "_id" : ObjectId("5d52ffa62daf944acd309a03"),
        "name" : "Leo",
        "location" : "Chicago"
}
{
        "_id" : ObjectId("5d52ffbc2daf944acd309a04"),
        "name" : "Marshal",
        "location" : "California"
}
>

The output contains all the documents except for the first five. This is how skip method works. Remember, we need to pass a positive integer to the skip function. If not, the output will contain all the documents of the present in the collection. Negative values will lead to an error.

Skip performance

The skip method is useful in various ways. But it has performance issues when there are a huge number of documents in a collection. When we skipped the first five documents, it did not mean, the find method reached the sixth document directly. It has to iterate over all the documents to reach the specified point and then it returns the documents. This is normal in a collection containing ten documents. But what happens when there are a thousand documents in a collection and we want to skip the first five hundred documents? It is costly, very costly. The skip method will become very slow.

The skip method does not use the indexes. It has to iterate over all the document before reaching the specified document. This is why the skip method is not recommended for a collection containing a huge number of documents. Although, it works fine for a smaller number of documents.

Conclusion

The skip method is useful while working on a collection containing a small number of documents. We can directly skip to the desired document. But in the case of tons of documents, its performance decreases. So it is not recommended to use the skip method when there are a huge amount of documents present in a collection.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.