Articles → MONGODB → $Project Aggregation Stage In Mongodb

$Project Aggregation Stage In Mongodb






Purpose




  1. Including or excluding fields
  2. Adding new fields
  3. Modify existing fields

Example




Picture showing the sample document collection in mongodb




db.Employees.aggregate([
  {
    $project: {
     _id:1, // Include    
      fullname : {$concat: ["$firstname"," ", "$lastname"]},// adding a new field
     EmployeeAge: "$age" // Rename
      
    }
  }
])



Output


Picture showing the output of $project aggregation function in mongodb



Posted By  -  Karan Gupta
 
Posted On  -  Monday, June 3, 2024

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250