Create groups of matching data with rules


This Content is from Stack Overflow. Question asked by David

I’m working in a project where people fill a form then with a Mongo aggregation that group all the people based on the date, time and place they choose.

const matchController = {
  generateMatch: async (req, res) => {
    const form = await Forms.aggregate([
        $group: {
          _id: { Date: "$date", Time: "$time", Place: "$place" },
          Data: {
            $addToSet: {
              Name: "$firstName",
              Surname: "$surname",
              Email: "$email",
              Date: "$date",
              Time: "$time",
              Status: "$status",
              Place: "$place",
              _id: "$_id"
          Total: { $sum: 1 },
        $match: {
          Total: { $gte: 2 },
      { $out: "matchs" },

But now I would like to add more complexity with some rules, for example I want each group to be made with just 2 filled forms. Also I’m thinking about allowing mutiple time selection so for example, 4 people fill the form with same date and place but:

Person 1 selects 08:00 a.M and 12:00p.M,
Person 2 selects 12:00 p.M,
Person 3 selects 8:00 a.M and 13:00 p.M,
Person 4 selects 12:00 p.m.

I want a validation so in this example instead of getting person 1 and 2 matching and person 3 and 4 free, it would check and match person 1 and 3 and person 2 and 4.

I know this question is kind of complex so any guidance on how to get there would be deeply appreciated and thanked.


This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?