Need help with data aggregation at MongoDB



  • Hello! I'm bringing an example of the data immediately (JSON is irrelevant, not important):

    {  
       "_id":ObjectId("57989a4eb9b56b535b29751d"),
       "subid":"adwords",
       "extrasubid":[  
          {  
             "subid":"ua",
             "_id":ObjectId("57989a566cb2296a5bc062fa"),
             "leadPrice":0,
             "revenue":0,
             "registrations":0,
             "hits":34
          },
          {  
             "subid":"ru",
             "_id":ObjectId("5798abcc4d31957714ca7736"),
             "uniques":0,
             "leadPrice":0,
             "revenue":0,
             "registrations":2,
             "hits":10
          }
       ],
       "__v":3
    }{  
       "_id":ObjectId("5798a25e213c708b793ed04b"),
       "subid":"google",
       "extrasubid":[  
          {  
             "subid":"google",
             "_id":ObjectId("5798a282213c708b793ed04d"),
             "leadPrice":0,
             "revenue":0,
             "registrations":0,
             "hits":9
          }
       ],
       "__v":1
    }
    

    So, in every collection, I need to add the amount. revenueregistrationshits each facility extrasubidto make it like this:

    {  
           "_id":ObjectId("57989a4eb9b56b535b29751d"),
           "subid":"adwords",
           "extrasubid":[  
              {  
                 "subid":"ua",
                 "_id":ObjectId("57989a566cb2296a5bc062fa"),
                 "leadPrice":0,
                 "revenue":0,
                 "registrations":0,
                 "hits":34
              },
              {  
                 "subid":"ru",
                 "_id":ObjectId("5798abcc4d31957714ca7736"),
                 "uniques":0,
                 "leadPrice":0,
                 "revenue":0,
                 "registrations":2,
                 "hits":10
              },
           ],
           revenue: 0,
           registrations: 2,
           hits: 44,
           "__v":3
        }{  
           "_id":ObjectId("5798a25e213c708b793ed04b"),
           "subid":"google",
           "extrasubid":[  
              {  
                 "subid":"google",
                 "_id":ObjectId("5798a282213c708b793ed04d"),
                 "leadPrice":0,
                 "revenue":0,
                 "registrations":0,
                 "hits":9
              }
           ],
           revenue: 0,
           registrations: 2,
           hits: 9,
           "__v":1
        }
    

    How am I trying to do this?

    Channel.aggregate([
        { $unwind: "$extrasubid" },
        { $project: {
            _id: "$id",
            subid: "$subid",
            hits: { $sum: "$extrasubid.hits"},
            registrations: { $sum: "$extrasubid.registrations"},
            revenue: { $sum: "$extrasubid.revenue"},
            leadPrice: { $sum: "$extrasubid.leadPrice"},
            uniques: { $sum: "$extrasubid.uniques"}
        }},
    ], function (err, results) {
        console.log(results, results.length);
    })
    

    But on the way out, I get three objects, that's, that's, that's, like, "extrasubid. How do you fix that and get the result as higher?



  • Problem solved by removal $unwind♪ The code is:

    Channel.aggregate([
        { $project: {
            _id: "$id",
            subid: "$subid",
            hits: { $sum: "$extrasubid.hits"},
            registrations: { $sum: "$extrasubid.registrations"},
            revenue: { $sum: "$extrasubid.revenue"},
            leadPrice: { $sum: "$extrasubid.leadPrice"},
            uniques: { $sum: "$extrasubid.uniques"}
        }},
    ], function (err, results) {
        console.log(results, results.length);
    })
    

    Sorry about the inconvenience.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2