MongoDB aggregate, how to split a group into a sub group?

Viewed 72

I was wondering how do I split a group into another sub group?

I have a group that I start by splitting by attribute of "" and then I want to split the results, for each new group, by another attribute of "device". And also limit the results of the seconds documents to only 2 items.

Not sure how to achieve that, could use some help.

A link for an example: https://mongoplayground.net/p/b1bDE2dfXF4

Written example:

    [
      {
        _id: "6052dfb2210b6d3fc82db40d",
        : "iPhone 8",
        : "https://www.google.com"
      },
      {
        _id: "6052f2678b6d705c1e57ee32",
        : "desktop",
        : "https://www.google.com"
      },
      {
        _id: "605302a0172d7c769fc0e751",
        : "iPhone 8",
        : "https://www.google.com/2nd-page"
      },
      {
        _id: "605302a0172d7c769fc0e751Z",
        : "desktop",
        : "https://www.google.com/2nd-page"
      }
    ]

With the query of:

    db.collection.aggregate([
      {
        $match: {
          $and: [
            {
              $and: [
                {
                  $or: [
                    {
                      : "https://www.google.com/2nd-page"
                    },
                    {
                      : "https://www.google.com"
                    }
                  ]
                },
                {
                  $or: [
                    {
                      : "desktop"
                    },
                    {
                      : "iPhone 8"
                    }
                  ]
                }
              ]
            }
          ]
        }
      },
      {
        $sort: {
          : 1,
          : 1
        }
      },
      {
        $group: {
          _id: "$",
          items: {
            $push: "$$ROOT"
          }
        }
      },
      {
        $group: {
          _id: "$",
          items: {
            $push: "$$ROOT"
          }
        }
      }
    ])

will return:

    [
      {
        "_id": null,
        "items": [
          {
            "_id": "https://www.google.com",
            "items": [
              {
                "_id": "6052f2678b6d705c1e57ee32",
                "": "desktop",
                "": "https://www.google.com"
              },
              {
                "_id": "6052dfb2210b6d3fc82db40d",
                "": "iPhone 8",
                "": "https://www.google.com"
              }
            ]
          },
          {
            "_id": "https://www.google.com/2nd-page",
            "items": [
              {
                "_id": "605302a0172d7c769fc0e751Z",
                "": "desktop",
                "": "https://www.google.com/2nd-page"
              },
              {
                "_id": "605302a0172d7c769fc0e751",
                "": "iPhone 8",
                "": "https://www.google.com/2nd-page"
              }
            ]
          }
        ]
      }
    ]

What I actually want to get:

    [
      {
        "_id": null,
        "items": [
          {
            "_id": "https://www.google.com",
            "items": [
                {
                    "_id": "desktop",
                    "items": [
                        {
                            "_id": "6052f2678b6d705c1e57ee32",
                            "": "desktop",
                            "": "https://www.google.com"
                        }
                    ]
                },
                {
                    "_id": "iPhone 8",
                    "items": [
                        {
                            "_id": "6052dfb2210b6d3fc82db40d",
                            "": "iPhone 8",
                            "": "https://www.google.com"
                        }
                    ]
                }
            ]
          },
          {
            "_id": "https://www.google.com/2nd-page",
            "items": [
                {
                    "_id": "desktop",
                    "items": [
                        {
                            "_id": "6052f2678b6d705c1e57ee32",
                            "": "desktop",
                            "": "https://www.google.com/2nd-page"
                        }
                    ]
                },
                {
                    "_id": "iPhone 8",
                    "items": [
                        {
                            "_id": "6052dfb2210b6d3fc82db40d",
                            "": "iPhone 8",
                            "": "https://www.google.com/2nd-page"
                        }
                    ]
                }
            ]
          },
        ]
      }
    ]
0 Answers
Related