Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Map inner object field #26

Closed
luixal opened this issue Jun 11, 2017 · 2 comments
Closed

Map inner object field #26

luixal opened this issue Jun 11, 2017 · 2 comments

Comments

@luixal
Copy link

luixal commented Jun 11, 2017

Hi,

Let's say I got this object structure in a collection:

{
  "_id" : "gxDT7d49mgPh65h9P",
  "serviceTypeId" : "ayNc88uLkwZT9kn3m",
  "status" : "Done",
  "projectId" : "g8yCFambJxwFxY6Ms",
  "employee" : {
    "name" : "Adán",
    "lastname" : "Enríquez"
  },
  "serviceType" : {
    "name" : "Tipo2"
  },
  "dateUpdated" : ISODate("2017-04-30T10:48:06.065Z"),
  "plannedStartDate" : ISODate("2017-04-30T10:47:00Z"),
  "plannedEndDate" : ISODate("2017-04-30T10:48:00Z"),
  "executedStartDate" : ISODate("2017-04-30T10:48:00Z"),
  "executedEndDate" : ISODate("2017-04-30T10:48:00Z")
}

I'm using aggregation to get items in a month, grouped by serviceTypeId and plannedStartDate or executedEndDate. I'm using something like this (no date is mandatory):

Meteor.publish("serviceTypesCounts", function(startDate, endDate) {
  // init dates to current month if not provided:
  startDate = startDate || moment().startOf('month').toDate();
  endDate = endDate || moment().endOf('month').toDate();
  // Remember, ReactiveAggregate doesn't return anything
  ReactiveAggregate(
    this,
    Services,
    [
      {
        $match: {
          // match current user's projectId and either planned or executed date exists, and any of those dates are inside startDate, endDate range:
          $and: [
            {projectId: getCurrentProjectId(this.userId)},
            // plannedStartDate or executedStart date exists:
            {$or: [
              {plannedStartDate: {$exists: true}},
              {executedStartDate: {$exists: true}}
            ]},
            // plannedStartDate is in startDate, endDate range or it doesn't exist and executedStartDate is:
            {$or: [
              {
                plannedStartDate: {
                  $gte: startDate,
                  $lte: endDate
                }
              },
              {
                $and: [
                  {
                    executedStartDate: {
                      $gte: startDate,
                      $lte: endDate
                    }
                  },
                  {
                    plannedStartDate: {
                      $exists: false
                    }
                  }
                ]
              }
            ]}
          ]
        }
      },
      {
        $group: {
          '_id': {
            serviceTypeId: "$serviceTypeId",

            year: {
              $cond: [
                { $ifNull: ['$plannedStartDate', 0] },
                { $year: '$plannedStartDate' },
                { $year: '$executedStartDate'}
              ]
            },

            month: {
              $cond: [
                { $ifNull: ['$plannedStartDate', 0] },
                { $month: '$plannedStartDate' },
                { $month: '$executedStartDate'}
              ]
            },

            day: {
              $cond: [
                { $ifNull: ['$plannedStartDate', 0] },
                { $dayOfMonth: '$plannedStartDate' },
                { $dayOfMonth: '$executedStartDate'}
              ]
            },



          },
          'count': { $sum: 1 }
        }
      },
      {
        $project: {
          _id: {
            $concat: [
              '$_id.serviceTypeId',
              '-',
              {$substr: ["$_id.year", 0, 4]},
              {$substr: ["$_id.month", 0, 2]},
              {$substr: ["$_id.day", 0, 2]}
            ]
          },
          year: '$_id.year',
          month: '$_id.month',
          day: '$_id.day',
          count: '$count'
        } // Send the aggregation to the 'clientReport' collection available for client use
      }
    ],
    {
      clientCollection: "ServiceTypesCounts"
    }
  );
});

I works, I'm getting results like this:

{ _id: 'ayNc88uLkwZT9kn3m-2017521', count: 1, year: 2017, month: 5, day: 21 }
{ _id: 'YG873A6AZZ3eEAkqX-2017520', count: 1, year: 2017, month: 5, day: 20 }
{ _id: 'ayNc88uLkwZT9kn3m-2017520', count: 1, year: 2017, month: 5, day: 20 }
{ _id: 'YG873A6AZZ3eEAkqX-201757', count: 26, year: 2017, month: 5, day: 7 }
{ _id: 'YG873A6AZZ3eEAkqX-201758', count: 340, year: 2017, month: 5 ,day: 8 }

Now, thing is: I want to get field serviceType.name inside original objects (or event the full object), how can a I do this?

It would be great to get that field even if I'm not grouping by it, but in this case I don't care as it is the readable representation for serviceTypeId.

Thanks!

@JcBernack
Copy link
Owner

Hi,
this is not really a question about this package, but about Mongo aggregation. You should repost the question on a Mongo related board.

@luixal
Copy link
Author

luixal commented Jun 15, 2017

It really is, sorry :)

@luixal luixal closed this as completed Jun 15, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants