I am using mongo Atlas M10. I want to transform all document data to formatted tree data by using the aggregate framework. It is only working for a certain limit of documents.
I am getting below error in a large number of documents.
“MongoError: BSONObj size: 20726581 (0x13C4335) is invalid. Size must be between 0 and 16793600(16MB)”
I already set allowDiskUse to true. It is still getting that error.
May I have a solution for that error?
below are my aggregate stages:
model.aggregate([
{
$match: {
parent: 0
}
},
{
$graphLookup: {
from: appId + "_" + viewName + "s",
startWith: "$id",
connectFromField: "id",
connectToField: "parent",
depthField: "level",
as: "data"
}
},
{
$unset: [
"data._id",
"data.createdAt",
"data.updatedAt",
"data.updateBy"
]
},
{
$unwind: {
path: "$data",
preserveNullAndEmptyArrays: true
}
},
{
$sort: {
"data.level": -1
}
},
{
$group: {
_id: "$id",
parent: {
$first: "$parent"
},
value: {
$first: "$value"
},
type: {
$first: "$type"
},
data: {
$push: "$data"
}
}
},
{
$addFields: {
data: {
$reduce: {
input: "$data",
initialValue: {
level: -1,
presentData: [],
prevData: []
},
in: {
$let: {
vars: {
prev: {
$cond: [
{
$eq: [
"$$value.level",
"$$this.level"
]
},
"$$value.prevData",
"$$value.presentData"
]
},
current: {
$cond: [
{
$eq: [
"$$value.level",
"$$this.level"
]
},
"$$value.presentData",
[]
]
}
},
in: {
level: "$$this.level",
prevData: "$$prev",
presentData: {
$concatArrays: [
"$$current",
[
{
$mergeObjects: [
"$$this",
{
data: {
$filter: {
input: "$$prev",
as: "e",
cond: {
$eq: [
"$$e.parent",
"$$this.id"
]
}
}
}
}
]
}
]
]
}
}
}
}
}
}
}
},
{
$addFields: {
data: "$data.presentData"
}
}
]).allowDiskUse(true)