Introduction to MongoDB cursor.map() Method

MongoDB is a document-based distributed database that provides rich data manipulation methods. Among them, map() is one of the commonly used methods in MongoDB, which can map query results to a new array.

Syntax

The syntax of the map() method is as follows:

db.collection.find().map(function(doc) { ... })

Here, db.collection.find() represents the query result. The map() method will pass each document in the query result as a parameter to the callback function. In the callback function, the document can be manipulated, and the operation result can be saved to a new array.

Use cases

The map() method can be used to map query results to a new array, commonly used in data transformation, filtering, and other scenarios.

Examples

Here are two examples of using the map() method to illustrate its usage and effects.

Example 1

Suppose we have a MongoDB collection students that contains student score information, where each document contains three fields: student name, math score, and English score. We want to calculate the total score of each student and sort it from high to low. The code is as follows:

var result = db.students
  .find()
  .map(function (doc) {
    return {
      name: doc.name,
      totalScore: doc.mathScore + doc.englishScore
    }
  })
  .sort(function (a, b) {
    return b.totalScore - a.totalScore
  })

printjson(result)

In the above code, we first use the find() method to query all documents, and then use the map() method to map each document to a new document that contains the student’s name and total score. Finally, we use the sort() method to sort the query result and output the result to the console.

Example 2

Suppose we have a MongoDB collection employees that contains information for all employees, where each document contains three fields: employee name, hire date, and salary. We want to filter out employees who were hired after 2022 and save their name and salary information to a new array. The code is as follows:

var result = db.employees.find().map(function (doc) {
  if (doc.hireDate.getFullYear() > 2022) {
    return {
      name: doc.name,
      salary: doc.salary
    }
  }
})

printjson(result)

In the above code, we first use the find() method to query all documents, and then use the map() method to map each document that meets the condition to a new document that contains the employee’s name and salary. Finally, we output the result to the console.

Conclusion

Through this article, we have learned the basic syntax, use cases, examples, and conclusion of MongoDB’s map() method. The map() method can map query results to a new array, commonly used in data transformation, filtering, and other scenarios. At the same time, we need to be aware that when using the map() method, we should try to avoid modifying the original data to avoid unforeseen consequences. Therefore, when manipulating data in the callback function, a new array is usually created to store the modified data instead of directly modifying the original array.