Naresh G
Naresh G

Reputation: 83

mongo db aggregation how to get all fields

I am trying to write the aggregation total average duration.
I got the output but not coming all fields. How to get all fields in the results?

Any one please suggest me.

db.lights.aggregate(
   { 
     $match: { 
       CREATE_DATE: { 
         $gte: ISODate("2018-01-24T20:05:30.000Z"),
         $lt: ISODate("2018-02-24T20:05:30.000Z") 
       }  
     } 
   },{ $addFields: { 
      offduration: { 
        $divide: [
          { $subtract: ["$RECEIVEDDATE", "$CREATE_DATE"] }, 
                     3600000
          ]         
        }     
     } 
   }, { "$group": { 
      _id: { 
            SWITCHID: "$SWITCHID", 
            STATUS: "$STATUS"         
      },         
      avgduration: { $avg: "$offduration" },         
      SWITCHID: { $first: "$SWITCHID" },         
      CREATE_DATE: { $first: "$CREATE_DATE" },         
      RECEIVEDDATE: { $first: "$RECEIVEDDATE" },         
      STATUS: { $first: "$STATUS" },         
      offduration: { $first: "$offduration" },     
    } }, 
    { $project: {         
      _id: 0,         
      SWITCHID: 1,         
      CREATE_DATE: 1,         
      RECEIVEDDATE: 1,         
      STATUS: 1,         
      avgduration: '$avgduration',         
      offduration: '$offduration'     
    } },
    {"$group" : {     
      _id: { SWITCHID: "$SWITCHID" },     
      on_minus_off: { 
        $sum:{ "$cond": [ 
          { "$eq": ["$STATUS", "LIGHTS OFF"] },         
            "$avgduration", 
          { $subtract: [ 0, "$avgduration" ] }  
             ]
        }
      } 
    }
  }
)

Upvotes: 1

Views: 68

Answers (1)

Selva Kumar
Selva Kumar

Reputation: 849

You want to add "$project" for the required field in the first pipeline. then you want to apply in $group pipeline like this,

 $project: {
    requireFiled1 : "$requireFiled1",
    requireFiled2  : "$requireFiled2",  }

// And add the below in $group

    requiredField1 : {
        $first: "$requiredField1" // Projected Name in Projection Section 
                   },
db.lights.aggregate({
        $match: {
            CREATE_DATE: {
                $gte: ISODate("2018-01-24T20:05:30.000Z"),
                $lt: ISODate("2018-02-24T20:05:30.000Z")
            }
        }
    },
    {
        $addFields: {
            offduration: {
                $divide: [
                    {
                        $subtract: [
                            "$RECEIVEDDATE",
                            "$CREATE_DATE"
                        ]
                    },
                    3600000
                ]
            }
        }
    },
    {
        "$group": {
            _id: {
                SWITCHID: "$SWITCHID",
                STATUS: "$STATUS"
            },
            avgduration: {
                $avg: "$offduration"
            },
            SWITCHID: {
                $first: "$SWITCHID"
            },
            CREATE_DATE: {
                $first: "$CREATE_DATE"
            },
            RECEIVEDDATE: {
                $first: "$RECEIVEDDATE"
            },
            STATUS: {
                $first: "$STATUS"
            },
            offduration: {
                $first: "$offduration"
            },
            
        }
    },
    {
        $project: {
            _id: 0,
            SWITCHID: 1,
            CREATE_DATE: 1,
            RECEIVEDDATE: 1,
            STATUS: 1,
            avgduration: '$avgduration',
            offduration: '$offduration'
        }
    },
    {
        "$group": {
            _id: {
                SWITCHID: "$SWITCHID"
            },
           requireFiled1 : {
                $first: "$requireFiled1"
            },
           
            on_minus_off: {
                $sum: {
                    "$cond": [
                        {
                            "$eq": [
                                "$STATUS",
                                "LIGHTS OFF"
                            ]
                        },
                        "$avgduration",
                        {
                            $subtract: [
                                0,
                                "$avgduration"
                            ]
                        }
                    ]
                }
            }
        }
    })

Upvotes: 1

Related Questions