$project

noun

say it: “DOL-er proh-JEKT” /ˈdɑlər proʊˈdʒɛkt/

A MongoDB aggregation stage that picks, renames or removes the fields of each document passing through.

What does the $project stage do?

$project is an aggregation stage that reshapes each document: it keeps only the fields you name, can give a field a new name, and can leave out _id. It's often the last stage, to tidy a report's rows.

db.orders.aggregate([
  { $group: { _id: "$category", revenue: { $sum: "$total" } } },
  { $project: { _id: 0, category: "$_id", revenue: 1 } }
]);
// each row now has category and revenue, and no _id

In a sentence

  • “A final $project renamed _id to category and hid _id itself.”
  • “$project with revenue: 1 keeps the revenue field and drops every field it doesn't name.”

Level: Journeyman · More MongoDB terms at this level

Updated