Introduction to MongoDB $last Operator

$last is an operator in MongoDB’s aggregation pipeline that retrieves the value of the last element in an array. This operator can be used in a variety of scenarios, such as retrieving the latest article published by each author, or the most recent login time for each user.

Syntax

The syntax for the $last operator is as follows:

{ $last: <expression> }

Here, <expression> is the expression whose last element value is to be retrieved and can be any valid expression.

Usage

The $last operator is commonly used in the following scenarios:

  • Retrieving the value of the last element in an array.
  • Retrieving the value of a field in the last document after sorting by a certain field.

Examples

Retrieving the latest article published by each author

Assuming we have a collection called authors that stores information about authors and their articles, where each document contains the name and articles fields. Each articles field is an array containing the titles and publication dates of all the articles that the author has published. We can use the $last operator to retrieve the latest article published by each author.

db.authors.aggregate([
  {
    $project: {
      name: 1,
      latest_article: { $last: "$articles.title" },
      latest_article_date: { $last: "$articles.published_date" }
    }
  }
])

The above aggregation operation will return an array of documents, each containing the author’s name and the title and publication date of their latest article.

Retrieving the most recent login time for each user

Assuming we have a collection called user_login that stores information about user login times, where each document contains the user ID and login time, we can use the $last operator to retrieve the most recent login time for each user.

db.user_login.aggregate([
  {
    $group: {
      _id: "$user_id",
      last_login_time: { $last: "$login_time" }
    }
  }
])

The above aggregation operation will return an array of documents, each containing the user ID and their most recent login time.

Retrieving the latest exam scores for each class

Assuming we have a collection called class_scores that stores information about exam scores for different classes, where each document contains the class name, student name, and exam score, we can use the $last operator to retrieve the latest exam scores for each class.

db.class_scores.aggregate([
  {
    $sort: { exam_date: -1 }
  },
  {
    $group: {
      _id: "$class_name",
      student_scores: { $push: { name: "$student_name", score: "$score" } },
      latest_exam_date: { $last: "$exam_date" }
    }
  }
])

The above aggregation operation will return an array of documents, each containing the class name, exam scores for each student, and the latest exam date for the class.

Conclusion

The $last aggregation pipeline operator in MongoDB is used to retrieve the value of the last element in an array. This operator can be used in a variety of scenarios, such as retrieving the latest article published by each author or the most recent login time for each user.