Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Group By After LookUp

I have to count how many customers we have "Active" and "Inactive" in our system.
I could just group by Status and have it.
The problem is that each customer belongs to an office which might be "Inactive".

So, before knowing my customers’ Status, I need to match only the "Active" offices.
How can I do that?

Offices:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[
  {
    _id: UUID('d0ca792b-6ab3-464c-92ec-241be5551405'),
    Name: 'Office1',
    Status: 'Active'
  },
  {
    _id: UUID('1e49c08b-dfdb-4b29-aa01-63f32086647d'),
    Name: 'Office2',
    Status: 'Inactive'
  },
  {
    _id: UUID('d20d002f-a343-478b-9910-ceb2ee5d9fd3'),
    Name: 'Office3',
    Status: 'Active'
   }
]

Customers:

[
  {
    _id: UUID('10734c4f-c7c9-4db0-8bc6-2077cb85c242'),
    Status: 0,
    OfficeId: UUID('d0ca792b-6ab3-464c-92ec-241be5551405')
  },
  {
    _id: UUID('557fe764-c345-4fd7-94fa-086ce9e95415'),
    Status: 0,
    OfficeId: UUID('d20d002f-a343-478b-9910-ceb2ee5d9fd3')
  },
  {
    _id: UUID('b8998295-700b-451e-aff9-8c2a61ee25d3'),
    Status: 0,
    OfficeId: UUID('d0ca792b-6ab3-464c-92ec-241be5551405')
  },
  {
    _id: UUID('f90bce6d-a35d-4733-8d53-3085b15412d7'),
    Status: 1,
    OfficeId: UUID('d20d002f-a343-478b-9910-ceb2ee5d9fd3')
  },
  {
    _id: UUID('3023ad50-6dde-4c1b-b695-c75a147481e1'),
    Status: 0,
    OfficeId: UUID('d0ca792b-6ab3-464c-92ec-241be5551405')
  },
  {
    _id: UUID('74c46c51-6738-4eb8-ba47-a494189db5b4'),
    Status: 1,
    OfficeId: UUID('d20d002f-a343-478b-9910-ceb2ee5d9fd3')
  },
  {
    _id: UUID('447542b4-bf34-4a74-8168-c068248ae703'),
    Status: 1,
    OfficeId: UUID('1e49c08b-dfdb-4b29-aa01-63f32086647d')
  },
  {
    _id: UUID('aa461e7b-5a40-4e74-91f0-82c8df9c6106'),
    Status: 0,
    OfficeId: UUID('d20d002f-a343-478b-9910-ceb2ee5d9fd3')
  },
  {
    _id: UUID('06d224f1-fc7f-4395-934b-41f53bf6439b'),
    Status: 1,
    OfficeId: UUID('1e49c08b-dfdb-4b29-aa01-63f32086647d')
  },
  {
    _id: UUID('6bf2b36d-3470-4d1a-a50e-70b693b02ea5'),
    Status: 0,
    OfficeId: UUID('d20d002f-a343-478b-9910-ceb2ee5d9fd3')
  }
]

>Solution :

The main concept implementation:

  1. Join both "Customers" and "Offices" collections.
  2. Filter the documents with the office’s Status is "Active".

Solution 1:

db.Customers.aggregate([
  {
    $lookup: {
      from: "Offices",
      localField: "OfficeId",
      foreignField: "_id",
      as: "office"
    }
  },
  {
    $match: {
      "office.Status": "Active"
    }
  },
  {
    $unset: "office"
  }
])

Demo Solution 1 @ Mongo Playground


Solution 2:

db.Customers.aggregate([
  {
    $lookup: {
      from: "Offices",
      localField: "OfficeId",
      foreignField: "_id",
      pipeline: [
        {
          $match: {
            Status: "Active"
          }
        }
      ],
      as: "office"
    }
  },
  {
    $match: {
      "office": {
        $ne: []
      }
    }
  },
  {
    $unset: "office"
  }
])

Demo Solution 2 @ Mongo Playground

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading