Is it possible to populate nested arrays of object ids while preserving order? I understand that lookup is essentially random but is there a way to use the initial array of object ids to set the order for the populated ids after the lookups?
My initial model looks like this:
{
...,
guides: [
// array of object ids
]
}
and the final would look like
{
...,
guides: [
{
...,
sections: [
{
...
}
]
}
]
}
Current pipeline - works but items are unordered.
{
$match: {
$expr: {
$eq: ["$name", cookbookName],
},
},
},
{
$lookup: {
from: "guides",
localField: "guides",
foreignField: "_id",
as: "guides",
},
},
{
$unwind: {
path: "$guides",
},
},
{
$lookup: {
from: "sections",
localField: "guides.sections",
foreignField: "_id",
as: "guides.sections",
},
},
{
$group: {
_id: "$_id",
name: {
$first: "$name",
},
avatar_url: {
$first: "$avatar_url",
},
banner_url: {
$first: "$banner_url",
},
game: {
$first: "$game",
},
preview: {
$first: "$preview",
},
roles: {
$first: "$roles",
},
streams: {
$first: "$streams",
},
guides: {
$push: "$guides",
},
},
},