Getting random documents with aggregate $group

basically I’m getting random documents when I apply $group but I don’t want to get random documents for example
I have 6 documents like
1
2
3
4
5
6
if I apply groupby
1,2
3,5
4,6
let suppose these results after groupby
but when I run the query always the mongodb return random like
3,5
1,2
4,6
or
4.6
3,5
1,2
or
2,1
3,5
6,4

I’m using aggregates

this is the query
indent preformatted text by 4 spaces

                      {
      $lookup: {
        from: "products",
        let: { categoryId: "$_id", parentModel: "$parentModel" },
        pipeline: [
          {
            $match: {
              $expr: {
                $in: ["$$categoryId", "$category"],
              },
            },
            },                               
           {
            $sort: { price: 1 },
          },
          {
            $group: {
              _id: "$parentModel",
              parentModel: { $addToSet: "$parentModel" },
              category: { $first: "$category" },
              mainImage: { $first: "$mainImage" },
              slug: { $first: "$slug" },
              height: { $first: "$height" },
              width: { $first: "$width" },
              length: { $first: "$length" },
              price: { $first: "$price" },
              title: { $first: "$title" },
              groupedItem: { $first: "$groupedItem" }, 
              test: { $first: "$_id" }, 
            },
          },
          {
            $facet: {
              paginatedResults: [
                { $skip: skipPage },
                { $limit: PAGE_SIZE },
                {
                  $project: {
                    _id: "$test",
                    mainImage: 1,
                    slug: 1,
                    height: 1,
                    width: 1,
                    length: 1,
                    price: 1,
                    title: 1,
                    groupedItem: 1, 
                  }, 
                },
              ],
              totalCount: [
                {
                  $count: "count",
                },
              ],
            },
          },
        ],
        as: "products",
      },
    },

It will help us help you better if you could post real well formatted sample documents, with real well formatted sample desired output.

The main issues is that when we do not have real documents to work with it is often too time consuming to work on your problem and we simply skip over.

2 Likes
{
  $lookup: {
    from: "products",
    let: { categoryId: "$_id", parentModel: "$parentModel" },
    pipeline: [
      {
        $match: {
          $expr: {
            $in: ["$$categoryId", "$category"],
          },
        },
      },
      {
        $sort: { price: 1 },
      },
      {
        $group: {
          _id: "$parentModel",
          parentModel: { $addToSet: "$parentModel" },
          category: { $first: "$category" },
          mainImage: { $first: "$mainImage" },
          slug: { $first: "$slug" },
          height: { $first: "$height" },
          width: { $first: "$width" },
          length: { $first: "$length" },
          price: { $first: "$price" },
          title: { $first: "$title" },
          groupedItem: { $first: "$groupedItem" }, 
          test: { $first: "$_id" }, 
        },
      },
      {
        $facet: {
          paginatedResults: [
            { $skip: skipPage },
            { $limit: PAGE_SIZE },
            {
              $project: {
                _id: "$test",
                mainImage: 1,
                slug: 1,
                height: 1,
                width: 1,
                length: 1,
                price: 1,
                title: 1,
                groupedItem: 1, 
              }, 
            },
          ],
          totalCount: [
            {
              $count: "count",
            },
          ],
        },
      },
    ],
    as: "products",
  },
},

even i’m applying sorting but every time i’m getting random data

Sorry but without

and

it too much time consuming for me. Hopefully others will step in.