JIYIK CN >

Current Location:Home > Learning > DATABASE > MongoDB >

Get the last N records in MongoDB

Author:JIYIK Last Updated:2025/04/29 Views:

This article will explore the number of ways to get the last N records in MongoDB, where N is a positive number greater than zero. We will see how to retrieve the number of documents with and without sorting.

Get the last N records in MongoDB

We must have a collection with documents to proceed with the actual example. So, let’s go ahead and create a collection and insert some documents into it using the following queries.

Sample code:

> use get_n_records;
> db.createCollection('stock');
> db.stock.insertMany([
    {"item": "abc", "quantity": 12},
    {"item": "def", "quantity": 234},
    {"item": "ghi", "quantity": 45},
    {"item": "jkl", "quantity": 345},
    {"item": "mno", "quantity": 243},
    {"item": "pqr", "quantity": 345},
    {"item": "stu", "quantity": 56},
    {"item": "vwx", "quantity": 575},
    {"item": "yzz", "quantity": 398}
]);
> db.stock.find();

Output:

{ "_id" : ObjectId("629ac651b7e637e64d8e75fb"), "item" : "abc", "quantity" : 12 }
{ "_id" : ObjectId("629ac651b7e637e64d8e75fc"), "item" : "def", "quantity" : 234 }
{ "_id" : ObjectId("629ac651b7e637e64d8e75fd"), "item" : "ghi", "quantity" : 45 }
{ "_id" : ObjectId("629ac651b7e637e64d8e75fe"), "item" : "jkl", "quantity" : 345 }
{ "_id" : ObjectId("629ac651b7e637e64d8e75ff"), "item" : "mno", "quantity" : 243 }
{ "_id" : ObjectId("629ac651b7e637e64d8e7600"), "item" : "pqr", "quantity" : 345 }
{ "_id" : ObjectId("629ac651b7e637e64d8e7601"), "item" : "stu", "quantity" : 56 }
{ "_id" : ObjectId("629ac651b7e637e64d8e7602"), "item" : "vwx", "quantity" : 575 }
{ "_id" : ObjectId("629ac651b7e637e64d8e7603"), "item" : "yzz", "quantity" : 398 }

Now, we have our collection ready and populated. The key thing to consider is whether we want to get the last N records from the most recently inserted document to the least recently inserted document or vice versa.

Let us study each of the scenarios given below.

Get the last N records from the most recently inserted document to the most recently inserted document and vice versa

Before we proceed, let’s review these two terms to understand them easily:

Use cursor.sort()method to get last N records from most recently inserted document to least recently inserted document and vice versa

Sample code (LIFO):

> db.stock.find().sort({_id:-1}).limit(3);

Output:

{ "_id" : ObjectId("629ac651b7e637e64d8e7603"), "item" : "yzz", "quantity" : 398 }
{ "_id" : ObjectId("629ac651b7e637e64d8e7602"), "item" : "vwx", "quantity" : 575 }
{ "_id" : ObjectId("629ac651b7e637e64d8e7601"), "item" : "stu", "quantity" : 56 }

Sample code (FIFO):

> db.stock.find().sort({_id:1}).limit(3);

Output:

{ "_id" : ObjectId("629ac651b7e637e64d8e75fb"), "item" : "abc", "quantity" : 12 }
{ "_id" : ObjectId("629ac651b7e637e64d8e75fc"), "item" : "def", "quantity" : 234 }
{ "_id" : ObjectId("629ac651b7e637e64d8e75fd"), "item" : "ghi", "quantity" : 45 }

Here, we use sort()the method to sort the documents from the most recently inserted to the most recent inserted order (descending). So, before fetching any document (record) from the database, we apply sort()the method to the cursor.

sort()The field value pairs of the method are {field: value}, we can perform sorting on up to 32 keys. When using MongoDB, documents are not stored in the collection in the specified order.

That is why when we perform a sort on a specific field with duplicate values, documents with these values ​​may be returned in any order. In order to get a consistent sort, we must include at least one field that contains unique values.

The easiest way to ensure this is sort()to use _idthe field in the method.

limit(N)Used to get a specified number of documents. If we are interested in getting all the documents in sorted order, we can omit this function.

Alternatively, we can also use the following query without specific fields.

Sample code:

> db.stock.find().sort({$natural:1}).limit(3);

Output:

{ "_id" : ObjectId("629ac651b7e637e64d8e75fb"), "item" : "abc", "quantity" : 12 }
{ "_id" : ObjectId("629ac651b7e637e64d8e75fc"), "item" : "def", "quantity" : 234 }
{ "_id" : ObjectId("629ac651b7e637e64d8e75fd"), "item" : "ghi", "quantity" : 45 }

If the values ​​of are and $naturalrespectively , they work like FIFO and LIFO. If we have MongoDB 4.4 or higher, we can use it.1-1

Use the $sortand $limit(aggregate) stage to get the last N records from the most recently inserted document to the most recently inserted document and vice versa

Sample code (LIFO):

> db.stock.aggregate([
     { $sort : { _id : -1 } },
     { $limit : 4 }
]);

Output:

{ "_id" : ObjectId("629ac651b7e637e64d8e7603"), "item" : "yzz", "quantity" : 398 }
{ "_id" : ObjectId("629ac651b7e637e64d8e7602"), "item" : "vwx", "quantity" : 575 }
{ "_id" : ObjectId("629ac651b7e637e64d8e7601"), "item" : "stu", "quantity" : 56 }
{ "_id" : ObjectId("629ac651b7e637e64d8e7600"), "item" : "pqr", "quantity" : 345 }

Sample code (FIFO):

> db.stock.aggregate([
     { $sort : { _id : 1 } },
     { $limit : 4 }
]);

Output:

{ "_id" : ObjectId("629ac651b7e637e64d8e75fb"), "item" : "abc", "quantity" : 12 }
{ "_id" : ObjectId("629ac651b7e637e64d8e75fc"), "item" : "def", "quantity" : 234 }
{ "_id" : ObjectId("629ac651b7e637e64d8e75fd"), "item" : "ghi", "quantity" : 45 }
{ "_id" : ObjectId("629ac651b7e637e64d8e75fe"), "item" : "jkl", "quantity" : 345 }

$sortThe aggregation stage is similar to cursor.sort()the method. It is used to sort all the documents and return them to the pipeline in sorted order.

It requires a document with field names and the corresponding sort order. Remember that 1and -1are used for ascending and descending order.

As with cursor.sort()the method, we can use $sortthe aggregate stage to sort on up to 32 keys, with at least one field (containing unique values) to achieve a consistent sort order.

If we need to sort on multiple fields, the sort order is evaluated from left to right. $limittakes a number showing how many documents need to be printed on the screen; furthermore, it has a 64-bit integer limit.

Use the skip()and count()methods together to get the last N records in MongoDB

This approach is appealing because, given the project requirements, we are not sorting the data, but rather getting a segment of the document from the beginning or the end.

Sample code (get the first 3 records without sorting the documents):

> db.stock.find().limit(3);

Output:

{ "_id" : ObjectId("629ac651b7e637e64d8e75fb"), "item" : "abc", "quantity" : 12 }
{ "_id" : ObjectId("629ac651b7e637e64d8e75fc"), "item" : "def", "quantity" : 234 }
{ "_id" : ObjectId("629ac651b7e637e64d8e75fd"), "item" : "ghi", "quantity" : 45 }

Sample code (get the last 3 records without sorting the documents):

> db.stock.find().skip(db.stock.count() - 3)

Output:

{ "_id" : ObjectId("629ac651b7e637e64d8e7601"), "item" : "stu", "quantity" : 56 }
{ "_id" : ObjectId("629ac651b7e637e64d8e7602"), "item" : "vwx", "quantity" : 575 }
{ "_id" : ObjectId("629ac651b7e637e64d8e7603"), "item" : "yzz", "quantity" : 398 }

In this example, we use db.stock.count()to count the total number of documents, subtract 3 from it and use skip()the method to skip all documents.

Remember, we subtracted the number of documents that were not skipped (3 in this case). We will end up with those 3 documents.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

List all collections in MongoDB Shell

Publish Date:2025/04/29 Views:156 Category:MongoDB

When using MongoDB , there are several ways to list the collections in the database. This article will discuss four different ways to get a list of collections in a MongoDB database. These methods are as follows: show collections List all c

Querying for non-null values in MongoDB

Publish Date:2025/04/29 Views:139 Category:MongoDB

This MongoDB article will explain how to query for non-null values ​​in MongoDB. To query for non-null values, you can use $ne the operator and $eq the operator and then specify the desired value to query. This article shows the readers

Shutting down with code:100 error in MongoDB

Publish Date:2025/04/29 Views:53 Category:MongoDB

This MongoDB tutorial will teach you to fix the error on different operating systems shutting down with code:100 . It will also talk about the root cause of why this issue occurs. shutting down with code:100 Errors in MongoDB As we all know

SELECT COUNT GROUP BY in MongoDB

Publish Date:2025/04/29 Views:74 Category:MongoDB

In this article, we will discuss the functions in MongoDB. Also, we will point out the aggregation functions in detail. We will explain in detail the different ways to count and sort multiple and single fields of Group in MongoDB. Operation

Differences between MongoDB and Mongoose

Publish Date:2025/04/29 Views:80 Category:MongoDB

This MongoDB article will discuss the differences between MongoDB and Mongoose. Unfortunately, most beginners tend to confuse these two concepts when they start developing applications and use MongoDB as their backend. MongoDB has its own s

Install MongoDB using Homebrew

Publish Date:2025/04/29 Views:161 Category:MongoDB

MongoDB is a well-known unstructured database management system that can handle large amounts of data. It is a document-oriented database system and belongs to the NoSQL family (non-SQL). Data and records are stored as documents that look a

Create a MongoDB dump of the database

Publish Date:2025/04/29 Views:62 Category:MongoDB

In this MongoDB article, you’ll get a walkthrough of Mongodump and Mongorestore , how to use them, and some simple examples of backing up and restoring your collections using both tools. mongodump Commands in MongoDB Mongodump is a tool t

Get the size of the database in MongoDB

Publish Date:2025/04/29 Views:88 Category:MongoDB

When working in MongoDB, do you know the size of your database? Today, we will learn how to get the size of a database in MongoDB using show dbs the command and the method. db.stats() Get the size of the database in MongoDB We can show dbs;

Grouping values by multiple fields with MongoDB

Publish Date:2025/04/29 Views:99 Category:MongoDB

MongoDB Group by Multiple Fields is used to group values ​​by multiple fields using various methods. One of the most efficient ways to group various fields present in MongoDB documents is by using $group the operator, which helps in per

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial