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",
},
},